diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 2a931171aa..26cbc35c2c 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -74,6 +74,7 @@ jobs: env: BUILD_DIR: ${{ github.workspace }}/build + GOTOOLCHAIN: local SCCACHE_DIR: ${{ github.workspace }}/.sccache RUST_DIR: ${{ github.workspace }}/rxx RUSTC_WRAPPER: "sccache" @@ -105,6 +106,11 @@ jobs: - name: Set up Rust uses: dtolnay/rust-toolchain@stable + - name: Set up Go + uses: actions/setup-go@v7 + with: + go-version: '1.26.x' + - name: Install dependencies from brew run: | # Avoid reinstalling cmake diff --git a/.github/workflows/tics.yml b/.github/workflows/tics.yml index 6bbf46b3c3..a16ad0123c 100644 --- a/.github/workflows/tics.yml +++ b/.github/workflows/tics.yml @@ -21,6 +21,7 @@ jobs: COVERAGE_DIR: ${{ github.workspace }}/coverage NEEDRESTART_SUSPEND: yes DEBIAN_FRONTEND: noninteractive + GOTOOLCHAIN: local steps: - name: Checkout code @@ -51,6 +52,11 @@ jobs: - name: Install Rust toolchain uses: dtolnay/rust-toolchain@stable + - name: Set up Go + uses: actions/setup-go@v7 + with: + go-version: '1.26.x' + - name: Install cmake uses: lukka/get-cmake@v4.4.4 diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 6f19ffa4c3..9240229b97 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -73,6 +73,7 @@ jobs: env: BUILD_DIR: ${{ github.workspace }}/build + GOTOOLCHAIN: local SCCACHE_DIR: ${{ github.workspace }}/.sccache #RUSTC_WRAPPER: "sccache" Removed due to concurrency issues between C++ and Rust @@ -96,6 +97,11 @@ jobs: - name: Set up Rust uses: dtolnay/rust-toolchain@stable + - name: Set up Go + uses: actions/setup-go@v7 + with: + go-version: '1.26.x' + - name: Install other packages from Choco uses: crazy-max/ghaction-chocolatey@v4 with: diff --git a/3rd-party/vcpkg-port-utils/mingw/acquire-mingw-msys.cmake b/3rd-party/vcpkg-port-utils/mingw/acquire-mingw-msys.cmake new file mode 100644 index 0000000000..28d26026ed --- /dev/null +++ b/3rd-party/vcpkg-port-utils/mingw/acquire-mingw-msys.cmake @@ -0,0 +1,44 @@ +include("${CMAKE_CURRENT_LIST_DIR}/get-mingw-packages.cmake") + +# Acquire a shared MSYS2 root for all ports to avoid repeatedly setting up the +# same environment. +function(multipass_acquire_mingw_msys OUT_ROOT) + multipass_get_mingw_packages(MINGW_DIRECT_PACKAGES MINGW_URLS MINGW_SHA512S) + vcpkg_acquire_msys(MSYS_ROOT + PACKAGES findutils perl + DIRECT_PACKAGES ${MINGW_DIRECT_PACKAGES} + ) + + # vcpkg_acquire_msys() extracts the full MinGW packages but then deletes + # every mingw64/lib/pkgconfig on *every* call (the removal sits outside its + # "assemble the root once" guard). qemu-img's meson build needs the glib/zstd + # .pc files, so we keep a private backup of them (harvested once, gated on the + # backup dir) and copy it back into the canonical mingw64/lib/pkgconfig on + # every run. The canonical location matters: pkgconf's --define-prefix (on by + # default) only rewrites each .pc's absolute `prefix=/mingw64` into the real + # Windows path when the file is found under a `.../lib/pkgconfig` layout, and + # native mingw gcc can't resolve the bare `/mingw64` POSIX paths otherwise. + set(PKGCONFIG_BACKUP "${MSYS_ROOT}/.multipass-qemu-pkgconfig") + if(NOT EXISTS "${PKGCONFIG_BACKUP}") + # Re-extract the pinned packages into a scratch dir and copy out their + # .pc files (the headers/libs acquire already installed are untouched). + set(_harvest "${CURRENT_BUILDTREES_DIR}/pkgconfig-harvest") + file(REMOVE_RECURSE "${_harvest}") + list(LENGTH MINGW_URLS _mingw_count) + math(EXPR _mingw_last "${_mingw_count} - 1") + foreach(_i RANGE ${_mingw_last}) + list(GET MINGW_URLS ${_i} _url) + list(GET MINGW_SHA512S ${_i} _sha) + get_filename_component(_fn "${_url}" NAME) + vcpkg_download_distfile(_archive URLS "${_url}" FILENAME "${_fn}" SHA512 "${_sha}" QUIET) + file(ARCHIVE_EXTRACT INPUT "${_archive}" DESTINATION "${_harvest}") + endforeach() + file(GLOB _pc_files "${_harvest}/mingw64/lib/pkgconfig/*.pc") + file(COPY ${_pc_files} DESTINATION "${PKGCONFIG_BACKUP}") + file(REMOVE_RECURSE "${_harvest}") + endif() + file(GLOB _pc_backup "${PKGCONFIG_BACKUP}/*.pc") + file(COPY ${_pc_backup} DESTINATION "${MSYS_ROOT}/mingw64/lib/pkgconfig") + + set(${OUT_ROOT} "${MSYS_ROOT}" PARENT_SCOPE) +endfunction() diff --git a/3rd-party/vcpkg-ports/qemu/generate-mingw-lockfile.py b/3rd-party/vcpkg-port-utils/mingw/generate-mingw-lockfile.py similarity index 100% rename from 3rd-party/vcpkg-ports/qemu/generate-mingw-lockfile.py rename to 3rd-party/vcpkg-port-utils/mingw/generate-mingw-lockfile.py diff --git a/3rd-party/vcpkg-port-utils/mingw/get-mingw-packages.cmake b/3rd-party/vcpkg-port-utils/mingw/get-mingw-packages.cmake new file mode 100644 index 0000000000..86418e6430 --- /dev/null +++ b/3rd-party/vcpkg-port-utils/mingw/get-mingw-packages.cmake @@ -0,0 +1,26 @@ +# Reads the pinned MinGW closure into the requested output variables. +function(multipass_get_mingw_packages OUT_DIRECT_PACKAGES OUT_URLS OUT_SHA512S) + if(NOT ARGC EQUAL 3) + message(FATAL_ERROR "multipass_get_mingw_packages requires direct-packages, URLs, and SHA512 output variables") + endif() + + set(_direct_packages "") + set(_urls "") + set(_sha512s "") + file(STRINGS "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/mingw64-toolchain.lock" _lock_lines) + foreach(_line IN LISTS _lock_lines) + if(_line MATCHES "^#" OR _line STREQUAL "") + continue() + endif() + if(NOT _line MATCHES "^([^ \t]+)[ \t]+([0-9a-fA-F]+)$") + message(FATAL_ERROR "Malformed mingw64-toolchain.lock entry: ${_line}") + endif() + list(APPEND _direct_packages "${CMAKE_MATCH_1}" "${CMAKE_MATCH_2}") + list(APPEND _urls "${CMAKE_MATCH_1}") + list(APPEND _sha512s "${CMAKE_MATCH_2}") + endforeach() + + set(${OUT_DIRECT_PACKAGES} "${_direct_packages}" PARENT_SCOPE) + set(${OUT_URLS} "${_urls}" PARENT_SCOPE) + set(${OUT_SHA512S} "${_sha512s}" PARENT_SCOPE) +endfunction() diff --git a/3rd-party/vcpkg-ports/qemu/mingw64-toolchain.lock b/3rd-party/vcpkg-port-utils/mingw/mingw64-toolchain.lock similarity index 100% rename from 3rd-party/vcpkg-ports/qemu/mingw64-toolchain.lock rename to 3rd-party/vcpkg-port-utils/mingw/mingw64-toolchain.lock diff --git a/3rd-party/vcpkg-ports/qemu/portfile.cmake b/3rd-party/vcpkg-ports/qemu/portfile.cmake index d5b9cc9620..7ee9a3ec3d 100644 --- a/3rd-party/vcpkg-ports/qemu/portfile.cmake +++ b/3rd-party/vcpkg-ports/qemu/portfile.cmake @@ -87,8 +87,8 @@ if(VCPKG_TARGET_IS_WINDOWS) # Windows: build only qemu-img (the sole QEMU tool Multipass uses here; no # system emulator). The GCC/MinGW toolchain is acquired hermetically via # vcpkg_acquire_msys() from packages pinned by URL + SHA512 -- vcpkg's own - # pinned msys shell layer plus the MinGW closure in mingw64-toolchain.lock - # (regenerate with generate-mingw-lockfile.py). + # pinned msys shell layer plus the shared MinGW closure in + # vcpkg-port-utils/mingw/mingw64-toolchain.lock. ########################################################################### if(NOT VCPKG_TARGET_ARCHITECTURE STREQUAL "x64") @@ -109,31 +109,8 @@ if(VCPKG_TARGET_IS_WINDOWS) set(${OUT_VAR} "${_p}" PARENT_SCOPE) endfunction() - # Pinned MinGW-w64 closure (regenerate with generate-mingw-lockfile.py). - # Each non-comment line is " ". Keep an interleaved list for - # vcpkg_acquire_msys(DIRECT_PACKAGES ...) plus parallel url/sha lists. - set(MINGW_DIRECT_PACKAGES "") - set(MINGW_URLS "") - set(MINGW_SHA512S "") - file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/mingw64-toolchain.lock" _lock_lines) - foreach(_line IN LISTS _lock_lines) - if(_line MATCHES "^#" OR _line STREQUAL "") - continue() - endif() - if(NOT _line MATCHES "^([^ \t]+)[ \t]+([0-9a-fA-F]+)$") - message(FATAL_ERROR "Malformed mingw64-toolchain.lock entry: ${_line}") - endif() - list(APPEND MINGW_DIRECT_PACKAGES "${CMAKE_MATCH_1}" "${CMAKE_MATCH_2}") - list(APPEND MINGW_URLS "${CMAKE_MATCH_1}") - list(APPEND MINGW_SHA512S "${CMAKE_MATCH_2}") - endforeach() - - # Assemble a private, content-addressed MSYS2 root: vcpkg's pinned msys shell - # utilities (plus findutils/perl for configure) and our pinned MinGW closure. - vcpkg_acquire_msys(MSYS_ROOT - PACKAGES findutils perl - DIRECT_PACKAGES ${MINGW_DIRECT_PACKAGES} - ) + include("${CMAKE_CURRENT_LIST_DIR}/../../vcpkg-port-utils/mingw/acquire-mingw-msys.cmake") + multipass_acquire_mingw_msys(MSYS_ROOT) # Run the MSYS2 tools under an explicit MINGW64 environment (no login shell): # mingw64/bin and usr/bin on PATH, MSYSTEM selecting the MinGW64 personality. @@ -156,37 +133,6 @@ if(VCPKG_TARGET_IS_WINDOWS) to_msys_path("${BUILD_DIR}" BUILD_DIR_UNIX) to_msys_path("${CURRENT_PACKAGES_DIR}" INSTALL_PREFIX_UNIX) - # vcpkg_acquire_msys() extracts the full MinGW packages but then deletes - # every mingw64/lib/pkgconfig on *every* call (the removal sits outside its - # "assemble the root once" guard). qemu-img's meson build needs the glib/zstd - # .pc files, so we keep a private backup of them (harvested once, gated on the - # backup dir) and copy it back into the canonical mingw64/lib/pkgconfig on - # every run. The canonical location matters: pkgconf's --define-prefix (on by - # default) only rewrites each .pc's absolute `prefix=/mingw64` into the real - # Windows path when the file is found under a `.../lib/pkgconfig` layout, and - # native mingw gcc can't resolve the bare `/mingw64` POSIX paths otherwise. - set(PKGCONFIG_BACKUP "${MSYS_ROOT}/.multipass-qemu-pkgconfig") - if(NOT EXISTS "${PKGCONFIG_BACKUP}") - # Re-extract the pinned packages into a scratch dir and copy out their - # .pc files (the headers/libs acquire already installed are untouched). - set(_harvest "${CURRENT_BUILDTREES_DIR}/pkgconfig-harvest") - file(REMOVE_RECURSE "${_harvest}") - list(LENGTH MINGW_URLS _mingw_count) - math(EXPR _mingw_last "${_mingw_count} - 1") - foreach(_i RANGE ${_mingw_last}) - list(GET MINGW_URLS ${_i} _url) - list(GET MINGW_SHA512S ${_i} _sha) - get_filename_component(_fn "${_url}" NAME) - vcpkg_download_distfile(_archive URLS "${_url}" FILENAME "${_fn}" SHA512 "${_sha}" QUIET) - file(ARCHIVE_EXTRACT INPUT "${_archive}" DESTINATION "${_harvest}") - endforeach() - file(GLOB _pc_files "${_harvest}/mingw64/lib/pkgconfig/*.pc") - file(COPY ${_pc_files} DESTINATION "${PKGCONFIG_BACKUP}") - file(REMOVE_RECURSE "${_harvest}") - endif() - file(GLOB _pc_backup "${PKGCONFIG_BACKUP}/*.pc") - file(COPY ${_pc_backup} DESTINATION "${MSYS_ROOT}/mingw64/lib/pkgconfig") - # meson build deps: distlib + QEMU's vendored meson wheel. site-packages # survives acquire's deletions, so this is a genuine one-time step. set(PYTHON_STAMP "${MSYS_ROOT}/.multipass-qemu-python-ready") @@ -199,7 +145,7 @@ if(VCPKG_TARGET_IS_WINDOWS) # skips the system emulator, --disable-docs avoids the sphinx dependency, and # pixman/zstd are pinned off/on so features don't depend on autodetection. # PKG_CONFIG_PATH/LIBDIR are cleared so pkgconf falls back to its default - # search (the canonical mingw64/lib/pkgconfig we restore above), which is what + # search (the canonical mingw64/lib/pkgconfig restored by the helper), which is what # triggers --define-prefix to emit real Windows include/lib paths. --prefix is # required (meson validates it) though we never install; the binary is copied # straight from the build tree. @@ -337,4 +283,4 @@ set(VCPKG_POLICY_EMPTY_INCLUDE_FOLDER enabled) # even for static-linkage triplets. set(VCPKG_POLICY_ALLOW_EXES_IN_BIN enabled) set(VCPKG_POLICY_DLLS_IN_STATIC_LIBRARY enabled) -vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/COPYING") \ No newline at end of file +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/COPYING") diff --git a/3rd-party/vcpkg-ports/ubuntu-insights/0001-c-api-free.patch b/3rd-party/vcpkg-ports/ubuntu-insights/0001-c-api-free.patch new file mode 100644 index 0000000000..80eed4d47e --- /dev/null +++ b/3rd-party/vcpkg-ports/ubuntu-insights/0001-c-api-free.patch @@ -0,0 +1,18 @@ +diff --git a/insights/C/libinsights.go b/insights/C/libinsights.go +--- a/insights/C/libinsights.go ++++ b/insights/C/libinsights.go +@@ -16,6 +16,14 @@ import ( + "github.com/ubuntu/ubuntu-insights/insights" + ) + ++// insights_free releases strings returned by the C API using the library's ++// allocator, including when the caller uses a different Windows C runtime. ++// ++//export insights_free ++func insights_free(ptr unsafe.Pointer) { ++ C.free(ptr) ++} ++ + /** + * insights_collect creates a report for the specified source. + * If config is NULL, defaults are used. diff --git a/3rd-party/vcpkg-ports/ubuntu-insights/portfile.cmake b/3rd-party/vcpkg-ports/ubuntu-insights/portfile.cmake new file mode 100644 index 0000000000..47f7ab34d6 --- /dev/null +++ b/3rd-party/vcpkg-ports/ubuntu-insights/portfile.cmake @@ -0,0 +1,151 @@ +set(VCPKG_POLICY_MISMATCHED_NUMBER_OF_BINARIES enabled) +vcpkg_check_linkage(ONLY_DYNAMIC_LIBRARY) + +# Using patchelf on the server tools causes them to crash +set(VCPKG_FIXUP_ELF_RPATH OFF) + +if(VCPKG_TARGET_IS_LINUX) + set(INSIGHTS_LIBRARY libinsights.so.1) +elseif(VCPKG_TARGET_IS_OSX) + set(INSIGHTS_LIBRARY libinsights.1.dylib) +elseif(VCPKG_TARGET_IS_WINDOWS) + if(NOT VCPKG_TARGET_ARCHITECTURE STREQUAL "x64") + message(FATAL_ERROR "ubuntu-insights on Windows requires x64 (the pinned MinGW closure is x86_64-only).") + endif() + set(INSIGHTS_LIBRARY libinsights.dll) +else() + message(FATAL_ERROR "ubuntu-insights supports Linux, macOS, and Windows targets.") +endif() + +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO ubuntu/ubuntu-insights + REF "insights/v${VERSION}" + SHA512 54420dd09b3fa4b96a989214fbef649e2e294149d8a9ed9638cec7eac55b8f49a572111376873eaee155cab0198ecfed846e8124f97a736062b22afd23917a6f + PATCHES 0001-c-api-free.patch +) + +# Ideally we should use `vcpkg_find_acquire_program` to automatically acquire go +# if it doesn't exist. Unfortunately the version provided by vcpkg (1.25) is less +# than the one required by ubuntu-insights (1.26). +find_program(GO_EXECUTABLE NAMES go HINTS ENV GO_EXECUTABLE_DIR REQUIRED) + +# Set up the common go environment that will be used to build the tools and the C-API +set(GO_ENV + CGO_ENABLED=1 + # An unset CGO_CFLAGS makes the upstream generator add -Werror and pedantic warnings + "CGO_CFLAGS=-O2 -g" + # Read by insights/C/generate to stamp the library version (defaults to "Dev") + "DEB_VERSION_UPSTREAM=${VERSION}" +) +if(VCPKG_TARGET_IS_OSX AND VCPKG_OSX_DEPLOYMENT_TARGET) + list(APPEND GO_ENV "MACOSX_DEPLOYMENT_TARGET=${VCPKG_OSX_DEPLOYMENT_TARGET}") +endif() + +# If on Windows, acquire MinGW which is required for building the C API. +# +# TODO: The acquired MinGW targets the legacy C runtime (CRT). Update it to use +# the UCRT packages instead. For now this should be ok. +if(VCPKG_TARGET_IS_WINDOWS) + include("${CMAKE_CURRENT_LIST_DIR}/../../vcpkg-port-utils/mingw/acquire-mingw-msys.cmake") + multipass_acquire_mingw_msys(MSYS_ROOT) + file(TO_NATIVE_PATH "${MSYS_ROOT}/mingw64/bin" MINGW_BIN) + list(APPEND GO_ENV + "CC=${MINGW_BIN}/gcc.exe" + --modify "PATH=path_list_prepend:${MINGW_BIN}" + ) + + set(C_API_ENV ${GO_ENV}) + set(INSIGHTS_DEF "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-insights.def") + list(APPEND C_API_ENV "CGO_LDFLAGS=-Wl,--output-def,${INSIGHTS_DEF}") +else() + set(C_API_ENV ${GO_ENV}) +endif() + +set(INSIGHTS_TOOLS "") + +# Build the tools if the feature is requested +if("tools" IN_LIST FEATURES) + function(add_insights_tool tool package) + vcpkg_execute_required_process( + COMMAND + "${CMAKE_COMMAND}" -E env ${GO_ENV} + "${GO_EXECUTABLE}" build -trimpath + -o "${tools_dir}/${tool}${VCPKG_TARGET_EXECUTABLE_SUFFIX}" "${package}" + WORKING_DIRECTORY "${SOURCE_PATH}" + LOGNAME "build-${tool}" + ) + list(APPEND INSIGHTS_TOOLS "${tool}") + set(INSIGHTS_TOOLS "${INSIGHTS_TOOLS}" PARENT_SCOPE) + endfunction() + + set(tools_dir "${CURRENT_PACKAGES_DIR}/tools/${PORT}") + file(MAKE_DIRECTORY "${tools_dir}") + + add_insights_tool(client ./insights/cmd/insights) + if(VCPKG_TARGET_IS_LINUX) + add_insights_tool(web-service ./server/cmd/web-service) + add_insights_tool(ingest-service ./server/cmd/ingest-service) + endif() +endif() + +# Build the C-API +vcpkg_execute_required_process( + COMMAND + "${CMAKE_COMMAND}" -E env ${C_API_ENV} + "${GO_EXECUTABLE}" run -tags=tools ./generate/... + WORKING_DIRECTORY "${SOURCE_PATH}/insights/C" + LOGNAME build-c-api +) + +if(VCPKG_TARGET_IS_WINDOWS) + # Generate an import library, otherwise MSVC will not be able to link against the generated dll + file(MAKE_DIRECTORY "${CURRENT_PACKAGES_DIR}/lib") + set(INSIGHTS_IMPLIB libinsights.lib) + find_program(lib_tool NAMES lib.exe llvm-lib REQUIRED) + vcpkg_execute_required_process( + COMMAND "${lib_tool}" "/def:${INSIGHTS_DEF}" /machine:x64 + "/out:${CURRENT_PACKAGES_DIR}/lib/${INSIGHTS_IMPLIB}" + WORKING_DIRECTORY "${CURRENT_BUILDTREES_DIR}" + LOGNAME import-library + ) + + file(INSTALL "${SOURCE_PATH}/insights/generated/${INSIGHTS_LIBRARY}" + DESTINATION "${CURRENT_PACKAGES_DIR}/bin") + + # libinsights is built with a single configuration, but vcpkg's applocal + # searches only debug/bin for DLLs when building Debug executables. + # Copy the DLL there as well so it can be deployed automatically. + # + # We also copy the lib import library so that vcpkg doesn't issue a warning + # for a dll without an accompany import library. + file(INSTALL "${SOURCE_PATH}/insights/generated/${INSIGHTS_LIBRARY}" + DESTINATION "${CURRENT_PACKAGES_DIR}/debug/bin") + file(INSTALL "${CURRENT_PACKAGES_DIR}/lib/${INSIGHTS_IMPLIB}" + DESTINATION "${CURRENT_PACKAGES_DIR}/debug/lib/") +else() + file(INSTALL "${SOURCE_PATH}/insights/generated/${INSIGHTS_LIBRARY}" + DESTINATION "${CURRENT_PACKAGES_DIR}/lib") +endif() + +file(INSTALL + "${SOURCE_PATH}/insights/generated/insights.h" + "${SOURCE_PATH}/insights/generated/types.h" + DESTINATION "${CURRENT_PACKAGES_DIR}/include/insights") + +configure_file("${CMAKE_CURRENT_LIST_DIR}/ubuntu-insights-config.cmake.in" + "${CURRENT_PACKAGES_DIR}/share/${PORT}/ubuntu-insights-config.cmake" @ONLY) + +include(CMakePackageConfigHelpers) +write_basic_package_version_file( + "${CURRENT_PACKAGES_DIR}/share/${PORT}/ubuntu-insights-config-version.cmake" + VERSION "${VERSION}" + COMPATIBILITY SameMajorVersion +) + +if("tools" IN_LIST FEATURES) + vcpkg_copy_tool_dependencies("${tools_dir}") +endif() +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE") + +file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}") diff --git a/3rd-party/vcpkg-ports/ubuntu-insights/ubuntu-insights-config.cmake.in b/3rd-party/vcpkg-ports/ubuntu-insights/ubuntu-insights-config.cmake.in new file mode 100644 index 0000000000..c3688c9d13 --- /dev/null +++ b/3rd-party/vcpkg-ports/ubuntu-insights/ubuntu-insights-config.cmake.in @@ -0,0 +1,32 @@ +if(TARGET ubuntu-insights::insights) + return() +endif() + +get_filename_component(_insights_prefix "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE) +add_library(ubuntu-insights::insights SHARED IMPORTED) +set_target_properties(ubuntu-insights::insights PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${_insights_prefix}/include" +) + +if(WIN32) + set_target_properties(ubuntu-insights::insights PROPERTIES + IMPORTED_LOCATION "${_insights_prefix}/bin/@INSIGHTS_LIBRARY@" + IMPORTED_LOCATION_DEBUG "${_insights_prefix}/debug/bin/@INSIGHTS_LIBRARY@" + IMPORTED_IMPLIB "${_insights_prefix}/lib/@INSIGHTS_IMPLIB@" + IMPORTED_IMPLIB_DEBUG "${_insights_prefix}/debug/lib/@INSIGHTS_IMPLIB@" + ) +else() + set_target_properties(ubuntu-insights::insights PROPERTIES + IMPORTED_LOCATION "${_insights_prefix}/lib/@INSIGHTS_LIBRARY@" + ) +endif() + +foreach(_insights_tool IN ITEMS @INSIGHTS_TOOLS@) + add_executable(ubuntu-insights::${_insights_tool} IMPORTED) + set_target_properties(ubuntu-insights::${_insights_tool} PROPERTIES + IMPORTED_LOCATION "${_insights_prefix}/tools/ubuntu-insights/${_insights_tool}@VCPKG_TARGET_EXECUTABLE_SUFFIX@" + ) +endforeach() + +unset(_insights_prefix) +unset(_insights_tool) diff --git a/3rd-party/vcpkg-ports/ubuntu-insights/usage b/3rd-party/vcpkg-ports/ubuntu-insights/usage new file mode 100644 index 0000000000..af5a4eb583 --- /dev/null +++ b/3rd-party/vcpkg-ports/ubuntu-insights/usage @@ -0,0 +1,16 @@ +ubuntu-insights provides a CMake package exposing its generated C API: + + find_package(ubuntu-insights CONFIG REQUIRED) + target_link_libraries(my_app PRIVATE ubuntu-insights::insights) + +Include the C API header with: + + #include + +The `ubuntu-insights::insights` target is always available. + +Enable the optional `tools` feature (`ubuntu-insights[tools]`) to build and expose the following executable targets: + + ubuntu-insights::client + ubuntu-insights::web-service (Linux only) + ubuntu-insights::ingest-service (Linux only) diff --git a/3rd-party/vcpkg-ports/ubuntu-insights/vcpkg.json b/3rd-party/vcpkg-ports/ubuntu-insights/vcpkg.json new file mode 100644 index 0000000000..76ffd7edcf --- /dev/null +++ b/3rd-party/vcpkg-ports/ubuntu-insights/vcpkg.json @@ -0,0 +1,13 @@ +{ + "name": "ubuntu-insights", + "version": "1.0.1", + "description": "Ubuntu Insights client, server executables, and C shared library", + "homepage": "https://github.com/ubuntu/ubuntu-insights", + "supports": "linux | osx | (windows & x64)", + "license": "GPL-3.0-only", + "features": { + "tools": { + "description": "Build the Ubuntu Insights client and server executables (servers are Linux-only)" + } + } +} diff --git a/3rd-party/vcpkg-triplets/x64-windows-static-md.cmake b/3rd-party/vcpkg-triplets/x64-windows-static-md.cmake index 4525507b59..0f380cbd65 100644 --- a/3rd-party/vcpkg-triplets/x64-windows-static-md.cmake +++ b/3rd-party/vcpkg-triplets/x64-windows-static-md.cmake @@ -2,3 +2,12 @@ set(VCPKG_TARGET_ARCHITECTURE x64) set(VCPKG_CRT_LINKAGE dynamic) set(VCPKG_LIBRARY_LINKAGE static) +# The shared MinGW closure lives outside the port directories, so add it to the +# ABI hash explicitly; otherwise lockfile bumps won't invalidate cached binaries. +if(PORT MATCHES "^(qemu|ubuntu-insights)$") + list(APPEND VCPKG_HASH_ADDITIONAL_FILES + "${CMAKE_CURRENT_LIST_DIR}/../vcpkg-port-utils/mingw/mingw64-toolchain.lock" + "${CMAKE_CURRENT_LIST_DIR}/../vcpkg-port-utils/mingw/get-mingw-packages.cmake" + "${CMAKE_CURRENT_LIST_DIR}/../vcpkg-port-utils/mingw/acquire-mingw-msys.cmake" + ) +endif() diff --git a/BUILD.linux.md b/BUILD.linux.md index 7decd0d89f..f4a9062928 100644 --- a/BUILD.linux.md +++ b/BUILD.linux.md @@ -31,6 +31,21 @@ sudo snap install rustup --classic rustup default stable ``` +### Install the Go compiler + +Building with Ubuntu Insights enabled (`MULTIPASS_ENABLE_INSIGHTS`) requires Go 1.26 or newer. +Install the Go snap: + +```sh +sudo snap install go --classic +``` + +Before running CMake, verify that the build terminal finds the required version: + +```sh +go version +``` + ## Building First, go into the repository root and get all the submodules: diff --git a/BUILD.macOS.md b/BUILD.macOS.md index 6d07cd4451..9ed6e9b3c2 100644 --- a/BUILD.macOS.md +++ b/BUILD.macOS.md @@ -47,6 +47,17 @@ To build multipass you will need the rust compiler. It can be installed using br brew install rustup ``` +### Go compiler + +Building with Ubuntu Insights enabled (`MULTIPASS_ENABLE_INSIGHTS`) requires Go 1.26 or newer. +Install Go using Homebrew: + +```sh +brew install go +``` + +Verify that `go version` reports Go 1.26 or newer before running CMake. + Building --------------------------------------- diff --git a/BUILD.windows.md b/BUILD.windows.md index c3d1183087..d11e8e37bc 100644 --- a/BUILD.windows.md +++ b/BUILD.windows.md @@ -34,6 +34,22 @@ compiler and related tooling or fix a broken `visualstudio2022buildtools` instal 6. Make sure that `vcpkg` is not installed in the same selection 7. Complete the installation +### Go compiler + +Building with Ubuntu Insights enabled (`MULTIPASS_ENABLE_INSIGHTS`) requires Go 1.26 or newer. +Install Go from an administrator PowerShell terminal: + +```[pwsh] +choco install golang -y +``` + +Open a new build terminal so it picks up the updated environment, and verify that it finds +Go 1.26 or newer before running CMake: + +```[pwsh] +go version +``` + ### Git You need to enable symlinks in Windows Git, have a look at diff --git a/feature-flags.cmake b/feature-flags.cmake index af86654a2a..8951cb2291 100644 --- a/feature-flags.cmake +++ b/feature-flags.cmake @@ -13,3 +13,26 @@ # along with this program. If not, see . include(src/cmake/feature-flag.cmake) + +feature_flag(MULTIPASS_ENABLE_INSIGHTS "Enable Ubuntu Insights") + +if(MULTIPASS_ENABLE_INSIGHTS) + list(APPEND VCPKG_MANIFEST_FEATURES "ubuntu-insights") + + # vcpkg uses an isolated build environment, so explicitly preserve Go's path. + find_program(GO_EXECUTABLE go REQUIRED) + get_filename_component(GO_EXECUTABLE_DIR "${GO_EXECUTABLE}" DIRECTORY) + + # Set GO_EXECUTABLE_DIR environment variable, and ask vcpkg to keep it. + # NOTE: The resulting ubuntu-insights ABI hash will not depend on the go toolchain + # used to build it. So if a new go compiler is installed, the port will not be rebuilt. + # This is fine for now. + set(ENV{GO_EXECUTABLE_DIR} "${GO_EXECUTABLE_DIR}") + + set(VCPKG_KEEP_ENV_VARS "$ENV{VCPKG_KEEP_ENV_VARS}") + list(APPEND VCPKG_KEEP_ENV_VARS GO_EXECUTABLE_DIR) + list(REMOVE_DUPLICATES VCPKG_KEEP_ENV_VARS) + + set(ENV{VCPKG_KEEP_ENV_VARS} "${VCPKG_KEEP_ENV_VARS}") + unset(VCPKG_KEEP_ENV_VARS) +endif() diff --git a/include/ubuntu_insights/libinsights_wrapper.h b/include/ubuntu_insights/libinsights_wrapper.h new file mode 100644 index 0000000000..4cd423df20 --- /dev/null +++ b/include/ubuntu_insights/libinsights_wrapper.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) Canonical, Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#pragma once + +#include + +// cgo generates C99 _Bool, which is not valid in C++. +#define _Bool bool +#include +#undef _Bool + +#define MP_LIBINSIGHTS multipass::LibInsights::instance() + +namespace multipass +{ +class LibInsights : public Singleton +{ +public: + LibInsights(const Singleton::PrivatePass& pass) noexcept; + + virtual void insights_free(void* ptr) const; + [[nodiscard]] virtual char* insights_collect(insights_const_config* config, + insights_const_char* source, + insights_const_collect_flags* flags, + char** out_report) const; + [[nodiscard]] virtual char* insights_compile(insights_const_config* config, + insights_const_compile_flags* flags, + char** out_report) const; + [[nodiscard]] virtual char* insights_write(insights_const_config* config, + insights_const_char* source, + insights_const_char* report, + insights_const_write_flags* flags) const; + [[nodiscard]] virtual char* insights_upload(insights_const_config* config, + insights_const_char** sources, + size_t sources_len, + insights_const_upload_flags* flags) const; + [[nodiscard]] virtual insights_consent_state + insights_get_consent_state(insights_const_config* config, insights_const_char* source) const; + [[nodiscard]] virtual char* insights_set_consent_state(insights_const_config* config, + insights_const_char* source, + bool new_state) const; + [[nodiscard]] virtual bool insights_get_system_opt_out_state( + insights_const_config* config) const; + [[nodiscard]] virtual char* insights_set_system_opt_out_state(insights_const_config* config, + bool state) const; + virtual void insights_set_log_callback(insights_logger_callback callback) const; +}; +} // namespace multipass diff --git a/packaging/gui-less/snap/snapcraft.yaml b/packaging/gui-less/snap/snapcraft.yaml index c10608ef58..0c2d5d4cfc 100644 --- a/packaging/gui-less/snap/snapcraft.yaml +++ b/packaging/gui-less/snap/snapcraft.yaml @@ -91,7 +91,7 @@ parts: plugin: cmake build-snaps: [] build-environment: - - PATH: /snap/bin:$PATH + - PATH: /usr/lib/go-1.26/bin:/snap/bin:$PATH - PYTHON: /usr/bin/python3 - PYTHONPATH: "" - VCPKG_FORCE_SYSTEM_BINARIES: 1 @@ -99,6 +99,7 @@ parts: - CLANG_VERSION: "19" - CXX: clang++-${CLANG_VERSION} - CC: clang-${CLANG_VERSION} + - GOTOOLCHAIN: local - CMAKE_PRESET: snap-gui-less build-packages: - on arm64: [libgles2-mesa-dev] @@ -132,6 +133,9 @@ parts: - bison # qtbase[dbus] -> glib2 -> gettext[tools] -> bison - gettext # provides msgfmt/msgmerge with matching libgettextsrc on the system loader path - rustup + # Required by ubuntu-insights + - golang-1.26-go + - libwayland-dev stage-packages: - apparmor @@ -144,6 +148,9 @@ parts: - libgtkmm-3.0-1t64 - libglibmm-2.4-1t64 - libpangomm-1.4-1v5 + # Required by ubuntu-insights + - libwayland-client0 + - x11-xserver-utils # ubuntu-insights uses xrandr from x11 utils stage: - -**/glvnd diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index f2f21faa82..d0d19eaf0a 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -113,7 +113,7 @@ parts: build-snaps: - cmake build-environment: - - PATH: /snap/bin:$PATH + - PATH: /usr/lib/go-1.26/bin:/snap/bin:$PATH - PYTHON: /usr/bin/python3 - PYTHONPATH: "" - VCPKG_FORCE_SYSTEM_BINARIES: 1 @@ -121,6 +121,7 @@ parts: - CLANG_VERSION: "19" - CXX: clang++-${CLANG_VERSION} - CC: clang-${CLANG_VERSION} + - GOTOOLCHAIN: local build-packages: - on arm64: [libgles2-mesa-dev] - build-essential @@ -153,6 +154,9 @@ parts: - bison # qtbase[dbus] -> glib2 -> gettext[tools] -> bison - gettext # provides msgfmt/msgmerge with matching libgettextsrc on the system loader path - rustup + # Required by ubuntu-insights + - golang-1.26-go + - libwayland-dev stage-packages: - apparmor @@ -166,6 +170,9 @@ parts: - libgtkmm-3.0-1t64 - libglibmm-2.4-1t64 - libpangomm-1.4-1v5 + # Required by ubuntu-insights + - libwayland-client0 + - x11-xserver-utils # ubuntu-insights uses xrandr from x11 utils stage: - -**/glvnd diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8ad417fefe..7f3b51e0d4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,6 +35,7 @@ add_subdirectory(settings) add_subdirectory(simplestreams) add_subdirectory(ssh) add_subdirectory(sshfs_mount) +add_subdirectory(ubuntu_insights) add_subdirectory(user_messages) add_subdirectory(utils) add_subdirectory(xz_decoder) diff --git a/src/ubuntu_insights/CMakeLists.txt b/src/ubuntu_insights/CMakeLists.txt new file mode 100644 index 0000000000..48b0cd6740 --- /dev/null +++ b/src/ubuntu_insights/CMakeLists.txt @@ -0,0 +1,30 @@ +# Copyright (C) Canonical, Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +if(NOT MULTIPASS_ENABLE_INSIGHTS) + return() +endif() + +add_library(ubuntu_insights STATIC libinsights_wrapper.cpp) + +find_package(ubuntu-insights CONFIG REQUIRED) + +target_link_libraries(ubuntu_insights PUBLIC ubuntu-insights::insights) +target_include_directories(ubuntu_insights PUBLIC "${CMAKE_SOURCE_DIR}/include") + +install(IMPORTED_RUNTIME_ARTIFACTS ubuntu-insights::insights + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib +) diff --git a/src/ubuntu_insights/libinsights_wrapper.cpp b/src/ubuntu_insights/libinsights_wrapper.cpp new file mode 100644 index 0000000000..49bb435fa1 --- /dev/null +++ b/src/ubuntu_insights/libinsights_wrapper.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (C) Canonical, Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include + +namespace multipass +{ +LibInsights::LibInsights(const Singleton::PrivatePass& pass) noexcept + : Singleton{pass} +{ +} + +void LibInsights::insights_free(void* ptr) const +{ + ::insights_free(ptr); +} + +char* LibInsights::insights_collect(insights_const_config* config, + insights_const_char* source, + insights_const_collect_flags* flags, + char** out_report) const +{ + return ::insights_collect(config, source, flags, out_report); +} + +char* LibInsights::insights_compile(insights_const_config* config, + insights_const_compile_flags* flags, + char** out_report) const +{ + return ::insights_compile(config, flags, out_report); +} + +char* LibInsights::insights_write(insights_const_config* config, + insights_const_char* source, + insights_const_char* report, + insights_const_write_flags* flags) const +{ + return ::insights_write(config, source, report, flags); +} + +char* LibInsights::insights_upload(insights_const_config* config, + insights_const_char** sources, + size_t sources_len, + insights_const_upload_flags* flags) const +{ + return ::insights_upload(config, sources, sources_len, flags); +} + +insights_consent_state LibInsights::insights_get_consent_state(insights_const_config* config, + insights_const_char* source) const +{ + return ::insights_get_consent_state(config, source); +} + +char* LibInsights::insights_set_consent_state(insights_const_config* config, + insights_const_char* source, + bool new_state) const +{ + return ::insights_set_consent_state(config, source, new_state); +} + +bool LibInsights::insights_get_system_opt_out_state(insights_const_config* config) const +{ + return ::insights_get_system_opt_out_state(config); +} + +char* LibInsights::insights_set_system_opt_out_state(insights_const_config* config, + bool state) const +{ + return ::insights_set_system_opt_out_state(config, state); +} + +void LibInsights::insights_set_log_callback(insights_logger_callback callback) const +{ + ::insights_set_log_callback(callback); +} +} // namespace multipass diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 064d73c52d..3c01075f53 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -32,12 +32,17 @@ target_link_libraries(multipass_integration_tests GTest::gmock_main Qt6::Core fmt::fmt-header-only libssh_wrapper + platform ) target_include_directories(multipass_integration_tests PRIVATE ${CMAKE_SOURCE_DIR} PRIVATE ${CMAKE_SOURCE_DIR}/src PRIVATE ${CMAKE_SOURCE_DIR}/src/platform/backends ) +if(MULTIPASS_ENABLE_INSIGHTS) + target_sources(multipass_integration_tests PRIVATE test_libinsights.cpp) + target_link_libraries(multipass_integration_tests PRIVATE ubuntu_insights) +endif() add_test(NAME multipass_integration_tests COMMAND multipass_integration_tests) ############################################################################## diff --git a/tests/unit/test_libinsights.cpp b/tests/unit/test_libinsights.cpp new file mode 100644 index 0000000000..ee14d00220 --- /dev/null +++ b/tests/unit/test_libinsights.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (C) Canonical, Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include + +#include + +#include "temp_dir.h" + +#include + +namespace multipass::test +{ +class LibInsights : public testing::Test +{ +protected: + const char* to_const_char_ptr(const char8_t* data) + { + return reinterpret_cast(data); + } + + void SetUp() override + { + std::filesystem::path tempdir_fs = tempdir; + + consent_dir = (tempdir_fs / "consent").u8string(); + insights_dir = (tempdir_fs / "insights").u8string(); + system_config_dir = (tempdir_fs / "system").u8string(); + + config = { + .consent_dir = to_const_char_ptr(consent_dir.c_str()), + .insights_dir = to_const_char_ptr(insights_dir.c_str()), + .system_config_dir = to_const_char_ptr(system_config_dir.c_str()), + .verbose = false, + }; + } + + TempDir tempdir; + std::u8string consent_dir; + std::u8string insights_dir; + std::u8string system_config_dir; + insights_config config{}; +}; + +TEST_F(LibInsights, missingConsentIsUnknown) +{ + EXPECT_EQ(MP_LIBINSIGHTS.insights_get_consent_state(&config, "multipass"), + INSIGHTS_CONSENT_UNKNOWN); +} + +TEST_F(LibInsights, consentCanBeCreatedAndUpdated) +{ + for (const auto consent : {true, false}) + { + char* error = MP_LIBINSIGHTS.insights_set_consent_state(&config, "multipass", consent); + + EXPECT_EQ(error, nullptr) << error; + EXPECT_EQ(MP_LIBINSIGHTS.insights_get_consent_state(&config, "multipass"), + consent ? INSIGHTS_CONSENT_TRUE : INSIGHTS_CONSENT_FALSE); + + MP_LIBINSIGHTS.insights_free(error); + } +} + +TEST_F(LibInsights, consentIsSpecificToSource) +{ + char* error = MP_LIBINSIGHTS.insights_set_consent_state(&config, "multipass", true); + + EXPECT_EQ(error, nullptr) << error; + EXPECT_EQ(MP_LIBINSIGHTS.insights_get_consent_state(&config, "other"), + INSIGHTS_CONSENT_UNKNOWN); + EXPECT_EQ(MP_LIBINSIGHTS.insights_get_consent_state(&config, "multipass"), + INSIGHTS_CONSENT_TRUE); + + MP_LIBINSIGHTS.insights_free(error); +} +} // namespace multipass::test diff --git a/vcpkg.json b/vcpkg.json index 3671752f95..83a2b28cff 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -59,6 +59,12 @@ "platform": "osx | linux" } ] + }, + "ubuntu-insights": { + "description": "Enable Ubuntu Insights integration", + "dependencies": [ + "ubuntu-insights" + ] } } }