diff --git a/build_library/build_image_util.sh b/build_library/build_image_util.sh index 119bfe28cf..7ea5318450 100755 --- a/build_library/build_image_util.sh +++ b/build_library/build_image_util.sh @@ -269,24 +269,76 @@ write_packages() { } # Generate a list of packages w/ their licenses in the format: -# sys-apps/systemd-212-r8::coreos GPL-2 LGPL-2.1 MIT public-domain +# [ +# { +# "project": "sys-apps/systemd-212-r8::coreos", +# "license": ["GPL-2", "LGPL-2.1", "MIT", "public-domain"] +# } +# ] write_licenses() { info "Writing ${2##*/}" - local pkg lic + echo -n "[" > "$2" + + local pkg pkg_sep for pkg in $(image_packages "$1" | sort); do - lic="$1/var/db/pkg/${pkg%%:*}/LICENSE" - if [[ -f "$lic" ]]; then - echo "$pkg $(< "$lic")" + local path="$1/var/db/pkg/${pkg%%:*}/LICENSE" + local lic_str + if [[ -f "$path" ]]; then + lic_str="$(< $path)" else # The package is not installed in $1 so get the license from # its ebuild - lic=$(portageq-${BOARD} metadata "${BOARD_ROOT}" ebuild \ - "${pkg%%:*}" LICENSE 2>/dev/null ||:) - if [[ -n "$lic" ]]; then - echo "$pkg $lic" + lic_str=$(portageq-${BOARD} metadata "${BOARD_ROOT}" ebuild \ + "${pkg%%:*}" LICENSE 2>/dev/null ||:) + if [[ -z "$lic_str" ]]; then + warn "No license found for ${pkg}" + continue fi fi - done > "$2" + + [[ -n $pkg_sep ]] && echo "," + [[ -z $pkg_sep ]] && echo + pkg_sep="true" + + # Build a list of the required licenses vs the one-of licenses + # For example: + # GPL-3+ LGPL-3+ || ( GPL-3+ libgcc libstdc++ ) FDL-1.3+ + # required: GPL-3+ LGPL-3+ FDL-1.3+ + # one-of: GPL-3+ libgcc libstdc++ + local req_lics=($(sed 's/|| ([^)]*)//' <<< $lic_str)) + local opt_lics=($(sed 's/.*|| (\([^)]*\)).*/\1/' <<< $lic_str)) + + # Pick one of the one-of licenses, preferring a GPL license. Otherwise, + # pick the first. + local opt_lic="" + local lic + for lic in ${opt_lics[*]}; do + if [[ $lic =~ "GPL" ]]; then + opt_lic=$lic; + break + fi; + done + if [[ -z $opt_lic ]]; then + opt_lic=${opt_lics[0]} + fi + + # Remove duplicate licenses + local lics=$(tr ' ' '\n' <<< "${req_lics[*]} ${opt_lic}" | sort --unique | tr '\n' ' ') + + echo -n " {\"project\": \"${pkg}\", \"licenses\": [" + + local lic_sep="" + for lic in ${lics[*]}; do + [[ -n $lic_sep ]] && echo -n ", " + lic_sep="true" + + echo -n "\"${lic}\"" + done + + echo -n "]}" + done >> "$2" + + echo -e "\n]" >> "$2" } # Add an entry to the image's package.provided diff --git a/build_library/dev_image_util.sh b/build_library/dev_image_util.sh index d4bfe2e4de..0d4d35b9cc 100755 --- a/build_library/dev_image_util.sh +++ b/build_library/dev_image_util.sh @@ -90,7 +90,7 @@ create_dev_image() { local root_fs_dir="${BUILD_DIR}/rootfs" local image_contents="${image_name%.bin}_contents.txt" local image_packages="${image_name%.bin}_packages.txt" - local image_licenses="${image_name%.bin}_licenses.txt" + local image_licenses="${image_name%.bin}_licenses.json" start_image "${image_name}" "${disk_layout}" "${root_fs_dir}" "${update_group}" diff --git a/build_library/ebuild_aci_util.sh b/build_library/ebuild_aci_util.sh index 91d78fd42d..73aedfba74 100644 --- a/build_library/ebuild_aci_util.sh +++ b/build_library/ebuild_aci_util.sh @@ -15,7 +15,7 @@ create_ebuild_aci_image() { local root_fs_dir="${BUILD_DIR}/rootfs" local image_contents="${image_name%.bin}_contents.txt" local image_packages="${image_name%.bin}_packages.txt" - local image_licenses="${image_name%.bin}_licenses.txt" + local image_licenses="${image_name%.bin}_licenses.json" start_image \ "${image_name}" "${disk_layout}" "${root_fs_dir}" "${update_group}" diff --git a/build_library/oem_aci_util.sh b/build_library/oem_aci_util.sh index 96ff564026..f6a1bbf176 100644 --- a/build_library/oem_aci_util.sh +++ b/build_library/oem_aci_util.sh @@ -22,7 +22,7 @@ create_oem_aci_image() { local root_fs_dir="${BUILD_DIR}/rootfs" local image_contents="${image_name%.bin}_contents.txt" local image_packages="${image_name%.bin}_packages.txt" - local image_licenses="${image_name%.bin}_licenses.txt" + local image_licenses="${image_name%.bin}_licenses.json" start_image \ "${image_name}" "${disk_layout}" "${root_fs_dir}" "${update_group}" diff --git a/build_library/prod_image_util.sh b/build_library/prod_image_util.sh index 8f67e28bcf..c742234173 100755 --- a/build_library/prod_image_util.sh +++ b/build_library/prod_image_util.sh @@ -65,7 +65,7 @@ create_prod_image() { local root_fs_dir="${BUILD_DIR}/rootfs" local image_contents="${image_name%.bin}_contents.txt" local image_packages="${image_name%.bin}_packages.txt" - local image_licenses="${image_name%.bin}_licenses.txt" + local image_licenses="${image_name%.bin}_licenses.json" local image_kernel="${image_name%.bin}.vmlinuz" local image_pcr_policy="${image_name%.bin}_pcr_policy.zip" local image_grub="${image_name%.bin}.grub"