Warning, /utilities/keysmith/src/secrets/CMakeLists.txt is written in an unsupported language. File is not indexed.

0001 #
0002 # SPDX-License-Identifier: BSD-2-Clause
0003 # SPDX-FileCopyrightText: 2020 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
0004 #
0005 
0006 set(secret_SRCS secrets.cpp)
0007 
0008 add_library(secrets_lib STATIC ${secret_SRCS})
0009 
0010 if (BUILD_EXTERNAL)
0011     message(STATUS "Will build external project: libsodium")
0012     include(ExternalProject)
0013 
0014     #
0015     # FIXME: for now assume that ANDROID means cross-compiling.
0016     # Technically this is not quite true when running on Android itself.
0017     #
0018     # As a work-around, a user may set an empty AUTOTOOLS_HOST to disable this.
0019     #
0020     if (ANDROID AND NOT DEFINED AUTOTOOLS_HOST)
0021         # see: https://developer.android.com/ndk/guides/other_build_systems
0022         set(AUTOTOOLS_HOST "")
0023         if ("${CMAKE_ANDROID_ARCH}" STREQUAL "arm")
0024             # note: this is the binutils name, armv7a-linux-androideabi is the name for clang
0025             set(AUTOTOOLS_HOST "arm-linux-androideabi")
0026         elseif ("${CMAKE_ANDROID_ARCH}" STREQUAL "arm64")
0027             set(AUTOTOOLS_HOST "aarch64-linux-android")
0028         elseif("${CMAKE_ANDROID_ARCH}" STREQUAL "x86")
0029             set(AUTOTOOLS_HOST "i686-linux-android")
0030         # not sure which will be passed, accept both
0031         elseif("${CMAKE_ANDROID_ARCH}" STREQUAL "x86_64" OR "${CMAKE_ANDROID_ARCH}" STREQUAL "x86-64")
0032             set(AUTOTOOLS_HOST "x86_64-linux-android")
0033         else()
0034             message(FATAL_ERROR "Building for Android but got an unknown/undefined/unsupported Android architecture (ANDROID_ARCH): '${ANDROID_ARCH}'")
0035         endif()
0036     endif()
0037 
0038     if (AUTOTOOLS_HOST)
0039         set(AUTOTOOLS_HOST_OPTION "--host=${AUTOTOOLS_HOST}")
0040     else()
0041         set(AUTOTOOLS_HOST_OPTION "")
0042     endif()
0043 
0044     set(sodium_LIBRARY_PATH "lib/libsodium.so")
0045     set(sodium_INCLUDE_PATH "include")
0046 
0047     #
0048     # Use a wrapper script for Android to pre-populate the environment with necessary environment variables.
0049     # This ensures the right toolchain will be picked up by the autotools build system.
0050     #
0051     set(ENV_WRAPPER "")
0052     if (ANDROID)
0053         set(ENV_WRAPPER "${CMAKE_BINARY_DIR}/android/android-export.sh")
0054     endif()
0055 
0056     #
0057     # Make gets confused if there is another make process somewhere in the process tree above it. This prevents it from
0058     # doing parallel make with the "jobserver is unavailable" warning instead of assuming it is an actual top-level make
0059     # process as it were.
0060     #
0061     # The work-around is to use $(MAKE), but that doesn't work if the parent process is not itself a make (e.g. ninja).
0062     # So the work-around for that is to use a wrapper script by default that does the right thing, except when CMake is
0063     # configured to use the Unix Makefiles generator in which case $(MAKE) should be used instead.
0064     #
0065     set(MAKE_SH "${CMAKE_SOURCE_DIR}/cmake/external/make.sh")
0066     if ("${CMAKE_GENERATOR}" STREQUAL "Unix Makefiles")
0067         set(MAKE_SH "$(MAKE)")
0068     endif()
0069     set(CONFIGURE_SH "${CMAKE_SOURCE_DIR}/cmake/external/configure-autotools.sh" "${AUTOTOOLS_HOST_OPTION}" "--prefix=<INSTALL_DIR>")
0070 
0071     #
0072     # Unfortunately CMake generates a build system (make/ninja) which does not check whether the library already exists.
0073     # This causes the external project to be fully rebuilt always. In turn, that triggers a rebuild of everything from
0074     # the external project upwards as well because the library file has changed.
0075     #
0076     # CHECK_SH is a prefix/wrapper for actual commands that short-circuits the build if the library already exists.
0077     #
0078     set(CHECK_SH test -e "<INSTALL_DIR>/${sodium_LIBRARY_PATH}" ||)
0079 
0080     externalProject_add(
0081         ext_libsodium
0082         PREFIX "${CMAKE_BINARY_DIR}/external"
0083         GIT_REPOSITORY https://github.com/jedisct1/libsodium.git
0084         GIT_TAG 1.0.18 # ${sodium_VERSION_STRING}
0085         GIT_SHALLOW ON
0086         GIT_PROGRESS ON
0087         CONFIGURE_COMMAND ${CHECK_SH} ${ENV_WRAPPER} ${CONFIGURE_SH}
0088         BUILD_COMMAND ${CHECK_SH} ${ENV_WRAPPER} ${MAKE_SH}
0089         BUILD_IN_SOURCE ON
0090         BUILD_ALWAYS OFF
0091         INSTALL_COMMAND ${CHECK_SH} ${ENV_WRAPPER} ${MAKE_SH} install
0092         # register the lib manually, otherwise cmake gets very confused about depending on this file
0093         BUILD_BYPRODUCTS <INSTALL_DIR>/${sodium_LIBRARY_PATH}
0094     )
0095 
0096     add_library(sodium SHARED IMPORTED)
0097 
0098     externalProject_get_property(ext_libsodium INSTALL_DIR)
0099 
0100     #
0101     # INTERFACE_INCLUDE_DIRECTORIES cannot take a non-existent directory. However, nearly everything external projects do
0102     # is done *after* cmake configure stage is over, so the directory simply won't be there.
0103     #
0104     # Apply the popular work-around and create it up front.
0105     #
0106     file(MAKE_DIRECTORY "${INSTALL_DIR}/${sodium_INCLUDE_PATH}")
0107     set_target_properties(sodium PROPERTIES IMPORTED_LOCATION "${INSTALL_DIR}/${sodium_LIBRARY_PATH}" INTERFACE_INCLUDE_DIRECTORIES "${INSTALL_DIR}/${sodium_INCLUDE_PATH}")
0108 
0109     add_dependencies(secrets_lib ext_libsodium)
0110 
0111     #
0112     # Add an explicit install rule for libsodium: without it a make/ninja install will not pick it up.
0113     #
0114     # Note that the androiddeployqt tooling is documented to explicitly require a make/ninja install step:
0115     # https://doc.qt.io/qt-5/deployment-android.html#package-template
0116     #
0117     install(FILES "${INSTALL_DIR}/${sodium_LIBRARY_PATH}" TYPE LIB)
0118 endif()
0119 
0120 target_link_libraries(secrets_lib Qt::Core sodium base32_lib)