From 8aa4d683026f47f2230fc8055a9727c4ff21d3af Mon Sep 17 00:00:00 2001 From: sylar Date: Fri, 14 Mar 2025 23:44:07 +0800 Subject: [PATCH] feat #42: support to build dylib for macos --- .github/workflows/ci-pr-on-main.yml | 12 +++++------ .github/workflows/custom-build.yml | 10 +++++++-- CMakeLists.txt | 6 +++++- scripts/build.bat | 16 ++++++-------- scripts/build.sh | 19 +++++++++++------ src/main/CMakeLists.txt | 33 ++++++++++++++++++----------- tests/smoke_test/CMakeLists.txt | 6 ++---- tests/unit_test/CMakeLists.txt | 6 ++---- 8 files changed, 63 insertions(+), 45 deletions(-) diff --git a/.github/workflows/ci-pr-on-main.yml b/.github/workflows/ci-pr-on-main.yml index 0ced631..0a7b499 100644 --- a/.github/workflows/ci-pr-on-main.yml +++ b/.github/workflows/ci-pr-on-main.yml @@ -38,8 +38,8 @@ jobs: -t ${{env.BUILD_TYPE}} -f ${{ steps.strings.outputs.build-dir }} -b ${{ steps.strings.outputs.build-output-dir }} - -U ON - -S ON + -U + -S - name: Unit Test working-directory: ${{ steps.strings.outputs.build-dir }}/linux/x86_64/tests/unit_test @@ -73,8 +73,8 @@ jobs: -t ${{env.BUILD_TYPE}} -f ${{ steps.strings.outputs.build-dir }} -b ${{ steps.strings.outputs.build-output-dir }} - -U ON - -S ON + -U + -S - name: Unit Test working-directory: ${{ steps.strings.outputs.build-dir }}\\x64\\tests/unit_test @@ -108,8 +108,8 @@ jobs: -t ${{env.BUILD_TYPE}} -f ${{ steps.strings.outputs.build-dir }} -b ${{ steps.strings.outputs.build-output-dir }} - -U ON - -S ON + -U + -S - name: Unit Test working-directory: ${{ steps.strings.outputs.build-dir }}/macos/tests/unit_test diff --git a/.github/workflows/custom-build.yml b/.github/workflows/custom-build.yml index 9cbdfbd..4f8d69c 100644 --- a/.github/workflows/custom-build.yml +++ b/.github/workflows/custom-build.yml @@ -176,8 +176,8 @@ jobs: -f ${{ steps.strings.outputs.build-dir }} -b ${{ steps.strings.outputs.build-output-dir }} - - name: Build for macOS iOS and xrOS - if: ${{ matrix.os == 'macos-latest' && (matrix.target_platform == 'macos' || matrix.target_platform == 'ios' || matrix.target_platform == 'xros') }} + - name: Build for iOS and xrOS + if: ${{ matrix.os == 'macos-latest' && (matrix.target_platform == 'ios' || matrix.target_platform == 'xros') }} run: > sh scripts/build.sh -p ${{ matrix.target_platform }} -v ${{ inputs.build_version }} @@ -185,6 +185,12 @@ jobs: -f ${{ steps.strings.outputs.build-dir }} -b ${{ steps.strings.outputs.build-output-dir }} + - name: Build for macOS + if: ${{ matrix.os == 'macos-latest' && (matrix.target_platform == 'macos') }} + run: | + sh scripts/build.sh -p ${{ matrix.target_platform }} -v ${{ inputs.build_version }} -t ${{ inputs.build_type }} -f ${{ steps.strings.outputs.build-dir }} -b ${{ steps.strings.outputs.build-output-dir }} + sh scripts/build.sh -p ${{ matrix.target_platform }} -v ${{ inputs.build_version }} -t ${{ inputs.build_type }} -f ${{ steps.strings.outputs.build-dir }} -b ${{ steps.strings.outputs.build-output-dir }} --no-framework + - name: Upload artifacts if: ${{ inputs.upload_artifacts == true }} uses: actions/upload-artifact@v4 diff --git a/CMakeLists.txt b/CMakeLists.txt index e976a86..ddc3989 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,10 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON) # Name project traa. project(traa C CXX) -message(STATUS "[TRAA] configuring...") +if(NOT DEFINED TRAA_OPTION_VERSION OR TRAA_OPTION_VERSION STREQUAL "") + set(TRAA_OPTION_VERSION "1.0.0") +endif() +message(STATUS "[TRAA] configuring ${TRAA_OPTION_VERSION}...") # CMAKE_SYSTEM_NAME only be set after project defined. message(STATUS "[TRAA] current target system name ${CMAKE_SYSTEM_NAME}") @@ -55,6 +58,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) # Options option(TRAA_OPTION_ENABLE_UNIT_TEST "Enable unit test" OFF) option(TRAA_OPTION_ENABLE_SMOKE_TEST "Enable smoke test" OFF) +option(TRAA_OPTION_NO_FRAMEWORK "Do not build framework for Apple platforms" OFF) if(LINUX) # To enable x11 you have install x11 and x11-xext and x11-xcomposite development packages. diff --git a/scripts/build.bat b/scripts/build.bat index a60c96c..cf7cd31 100644 --- a/scripts/build.bat +++ b/scripts/build.bat @@ -69,8 +69,8 @@ goto:eof echo -s, --source-dir Source directory [default: current directory] echo -v, --version Version number [default: 0.0.1] echo -V, --verbose Verbose output - echo -U, --unittest Build unit tests (ON/OFF) [default: OFF] - echo -S, --smoketest Build smoke tests (ON/OFF) [default: OFF] + echo -U, --unittest Build unit tests + echo -S, --smoketest Build smoke tests echo -h, --help Show this help message exit 0 goto:eof @@ -121,17 +121,13 @@ goto:eof ) else if "%arg%"=="--verbose" ( set "VERBOSE=1" ) else if "%arg%"=="-U" ( - set "BUILD_UNITTEST=%~1" - shift + set "BUILD_UNITTEST=ON" ) else if "%arg%"=="--unittest" ( - set "BUILD_UNITTEST=%~1" - shift + set "BUILD_UNITTEST=ON" ) else if "%arg%"=="-S" ( - set "BUILD_SMOKETEST=%~1" - shift + set "BUILD_SMOKETEST=ON" ) else if "%arg%"=="--smoketest" ( - set "BUILD_SMOKETEST=%~1" - shift + set "BUILD_SMOKETEST=ON" ) else if "%arg%"=="-h" ( call :show_usage ) else ( diff --git a/scripts/build.sh b/scripts/build.sh index f77229a..168e8e1 100644 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -18,6 +18,7 @@ BUILD_UNITTEST=OFF BUILD_SMOKETEST=OFF ANDROID_ABI="arm64-v8a,armeabi-v7a,x86,x86_64" TARGET_ARCH="x86_64" +NO_FRAMEWORK=OFF # cpu_features library on Android implements getauxval() for API level < 18, see cpu_features/src/hwcaps_linux_or_android.c # The implementation call dlopen() to load libc.so and then call dlsym() to get the address of getauxval(). @@ -45,13 +46,14 @@ show_usage() { echo " -b, --bin-folder Binary output folder [default: bin]" echo " -s, --source-dir Source directory [default: current directory]" echo " -v, --version Version of the build [default: 1.0.0]" - echo " -U, --unittest Build unit tests (ON/OFF) [default: OFF]" - echo " -S, --smoketest Build smoke tests (ON/OFF) [default: OFF]" + echo " -U, --unittest Build unit tests" + echo " -S, --smoketest Build smoke tests" echo " -A, --android-abi Android ABI [default: arm64-v8a,armeabi-v7a,x86,x86_64]" echo " -V, --verbose Verbose output" echo " -h, --help Show this help message" echo " -L, --api-level Android API level [default: 18]" echo " -a, --arch Target architecture for Linux (x86_64/aarch64_clang/aarch64_gnu) [default: x86_64]" + echo " --no-framework Do not build framework for Apple platforms" } # parse command line arguments @@ -82,12 +84,12 @@ while [[ $# -gt 0 ]]; do shift 2 ;; -U|--unittest) - BUILD_UNITTEST="$2" - shift 2 + BUILD_UNITTEST="ON" + shift ;; -S|--smoketest) - BUILD_SMOKETEST="$2" - shift 2 + BUILD_SMOKETEST="ON" + shift ;; -A|--android-abi) ANDROID_ABI="$2" @@ -109,6 +111,10 @@ while [[ $# -gt 0 ]]; do TARGET_ARCH="$2" shift 2 ;; + --no-framework) + NO_FRAMEWORK="ON" + shift + ;; *) log "ERROR" "Unknown option: $1" show_usage @@ -249,6 +255,7 @@ build() { -DTRAA_OPTION_ENABLE_UNIT_TEST="$BUILD_UNITTEST" \ -DTRAA_OPTION_ENABLE_SMOKE_TEST="$BUILD_SMOKETEST" \ -DTRAA_OPTION_VERSION="$VERSION" \ + -DTRAA_OPTION_NO_FRAMEWORK="$NO_FRAMEWORK" \ -S "$SOURCE_DIR" ;; "linux") diff --git a/src/main/CMakeLists.txt b/src/main/CMakeLists.txt index ac0fc1a..37bbc32 100644 --- a/src/main/CMakeLists.txt +++ b/src/main/CMakeLists.txt @@ -69,8 +69,8 @@ endif() # add compile definitions target_compile_definitions(${TRAA_LIBRARY_MAIN} PUBLIC TRAA_EXPORT) set_target_properties(${TRAA_LIBRARY_MAIN} PROPERTIES - VERSION "${TRAA_VERSION_SHORT}" - SOVERSION "${TRAA_VERSION_SHORT}" + VERSION "${TRAA_OPTION_VERSION}" + SOVERSION "${TRAA_OPTION_VERSION}" PUBLIC_HEADER "${TRAA_LIBRARY_PUBLIC_HEADER_FILES}" ARCHIVE_OUTPUT_DIRECTORY "${TRAA_ARCHIVE_OUTPUT_DIRECTORY}" LIBRARY_OUTPUT_DIRECTORY "${TRAA_ARCHIVE_OUTPUT_DIRECTORY}" @@ -86,26 +86,35 @@ if(APPLE) target_link_libraries(${TRAA_LIBRARY_MAIN} PRIVATE "${TRAA_LIBRARY_FRAMEWORKS}") set_target_properties(${TRAA_LIBRARY_MAIN} PROPERTIES - FRAMEWORK TRUE - FRAMEWORK_VERSION A - MACOSX_FRAMEWORK_IDENTIFIER "${TRAA_LIBRARY_IDENTIFIER}" - MACOSX_FRAMEWORK_SHORT_VERSION_STRING "${TRAA_VERSION_SHORT}" - MACOSX_FRAMEWORK_BUNDLE_VERSION "${TRAA_VERSION_SHORT}" + XCODE_ATTRIBUTE_ENABLE_BITCODE "NO" XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "${TRAA_LIBRARY_IDENTIFIER}" LINK_FLAGS "-Wl ${TRAA_LIBRARY_FRAMEWORKS_FOLDERS} -rpath @loader_path" ) - set_target_properties(${TRAA_LIBRARY_MAIN} PROPERTIES XCODE_ATTRIBUTE_ENABLE_BITCODE "NO") - + if(NOT TRAA_OPTION_NO_FRAMEWORK) + set_target_properties(${TRAA_LIBRARY_MAIN} PROPERTIES + FRAMEWORK TRUE + FRAMEWORK_VERSION A + MACOSX_FRAMEWORK_IDENTIFIER "${TRAA_LIBRARY_IDENTIFIER}" + MACOSX_FRAMEWORK_SHORT_VERSION_STRING "${TRAA_OPTION_VERSION}" + MACOSX_FRAMEWORK_BUNDLE_VERSION "${TRAA_OPTION_VERSION}" + ) + + set(TRAA_LIBRARY_TRUELY_OUTPUT_FILE "${TRAA_LIBRARY_MAIN_OUTPUT_NAME}.framework/${TRAA_LIBRARY_MAIN_OUTPUT_NAME}") + set(TRAA_LIBRARY_TRUELY_OUTPUT_FILE_DSYM "${TRAA_LIBRARY_MAIN_OUTPUT_NAME}.framework.dSYM") + else() + set(TRAA_LIBRARY_TRUELY_OUTPUT_FILE "lib${TRAA_LIBRARY_MAIN_OUTPUT_NAME}.${TRAA_OPTION_VERSION}.dylib") + set(TRAA_LIBRARY_TRUELY_OUTPUT_FILE_DSYM "${TRAA_LIBRARY_TRUELY_OUTPUT_FILE}.dSYM") + endif() # dsymutil to generate dsym file add_custom_command( TARGET ${TRAA_LIBRARY_MAIN} POST_BUILD COMMAND dsymutil - "${TRAA_ARCHIVE_OUTPUT_DIRECTORY}/${TRAA_LIBRARY_MAIN_OUTPUT_NAME}.framework/${TRAA_LIBRARY_MAIN_OUTPUT_NAME}" + "${TRAA_ARCHIVE_OUTPUT_DIRECTORY}/${TRAA_LIBRARY_TRUELY_OUTPUT_FILE}" "-o" - "${TRAA_ARCHIVE_OUTPUT_DIRECTORY}/${TRAA_LIBRARY_MAIN_OUTPUT_NAME}.framework.dSYM" + "${TRAA_ARCHIVE_OUTPUT_DIRECTORY}/${TRAA_LIBRARY_TRUELY_OUTPUT_FILE_DSYM}" COMMENT "Generate dsym file done on final binary." ) @@ -114,7 +123,7 @@ if(APPLE) TARGET ${TRAA_LIBRARY_MAIN} POST_BUILD COMMAND strip -r -S -x - "${TRAA_ARCHIVE_OUTPUT_DIRECTORY}/${TRAA_LIBRARY_MAIN_OUTPUT_NAME}.framework/${TRAA_LIBRARY_MAIN_OUTPUT_NAME}" + "${TRAA_ARCHIVE_OUTPUT_DIRECTORY}/${TRAA_LIBRARY_TRUELY_OUTPUT_FILE}" COMMENT "Strip debug and local symbols done on final binary." ) elseif(WIN32) diff --git a/tests/smoke_test/CMakeLists.txt b/tests/smoke_test/CMakeLists.txt index 32a9dc7..e6bcec3 100644 --- a/tests/smoke_test/CMakeLists.txt +++ b/tests/smoke_test/CMakeLists.txt @@ -57,12 +57,10 @@ if(APPLE) target_link_libraries(${TRAA_SMOKE_TEST} PRIVATE "${TRAA_LIBRARY_FRAMEWORKS}") set_target_properties(${TRAA_SMOKE_TEST} PROPERTIES - FRAMEWORK TRUE - FRAMEWORK_VERSION A MACOSX_BUNDLE_INFO_PLIST "${CMAKE_HOME_DIRECTORY}/src/base/devices/screen/test/simple_window/mac/info.plist" MACOSX_FRAMEWORK_IDENTIFIER "${TRAA_SMOKE_TEST_IDENTIFIER}" - MACOSX_FRAMEWORK_SHORT_VERSION_STRING "${TRAA_VERSION_SHORT}" - MACOSX_FRAMEWORK_BUNDLE_VERSION "${TRAA_VERSION_SHORT}" + MACOSX_FRAMEWORK_SHORT_VERSION_STRING "${TRAA_OPTION_VERSION}" + MACOSX_FRAMEWORK_BUNDLE_VERSION "${TRAA_OPTION_VERSION}" XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "${TRAA_SMOKE_TEST_IDENTIFIER}" CXX_VISIBILITY_PRESET hidden LINK_FLAGS "-Wl ${TRAA_SMOKE_TEST_FRAMEWORKS_FOLDERS} -rpath @loader_path" diff --git a/tests/unit_test/CMakeLists.txt b/tests/unit_test/CMakeLists.txt index c317ec2..37b55e5 100644 --- a/tests/unit_test/CMakeLists.txt +++ b/tests/unit_test/CMakeLists.txt @@ -168,12 +168,10 @@ if(APPLE) target_link_libraries(${TRAA_UNIT_TEST} PRIVATE "${TRAA_LIBRARY_FRAMEWORKS}") set_target_properties(${TRAA_UNIT_TEST} PROPERTIES - FRAMEWORK TRUE - FRAMEWORK_VERSION A MACOSX_BUNDLE_INFO_PLIST "${CMAKE_HOME_DIRECTORY}/src/base/devices/screen/test/simple_window/mac/info.plist" MACOSX_FRAMEWORK_IDENTIFIER "${TRAA_UNIT_TEST_IDENTIFIER}" - MACOSX_FRAMEWORK_SHORT_VERSION_STRING "${TRAA_VERSION_SHORT}" - MACOSX_FRAMEWORK_BUNDLE_VERSION "${TRAA_VERSION_SHORT}" + MACOSX_FRAMEWORK_SHORT_VERSION_STRING "${TRAA_OPTION_VERSION}" + MACOSX_FRAMEWORK_BUNDLE_VERSION "${TRAA_OPTION_VERSION}" XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "${TRAA_UNIT_TEST_IDENTIFIER}" CXX_VISIBILITY_PRESET hidden LINK_FLAGS "-Wl ${TRAA_UNIT_TEST_FRAMEWORKS_FOLDERS} -rpath @loader_path"