From 052fa7f4d17fb39b3d7de4fb0d0ba5f56897ad4c Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Mon, 22 Jul 2024 13:56:00 +0800 Subject: [PATCH] chore: update build script. - update CMake build script to make install statement is more legal. - add Windows-only build script for creating CMake used package and MSVC used package on Windows. - documentation will be added in the next commit. --- CMakeLists.txt | 35 ++++++++++---------- cmake/YYCCommonplaceConfig.cmake.in | 9 ++++-- doc/CMakeLists.txt | 2 +- script/{build.bat => win_build.bat} | 27 +++++++++++----- script/win_msvc_build.bat | 50 +++++++++++++++++++++++++++++ src/CMakeLists.txt | 8 ++--- testbench/CMakeLists.txt | 3 +- 7 files changed, 99 insertions(+), 35 deletions(-) rename script/{build.bat => win_build.bat} (51%) create mode 100644 script/win_msvc_build.bat diff --git a/CMakeLists.txt b/CMakeLists.txt index 3886886..dc2b3ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,19 +8,18 @@ project(YYCC option(YYCC_BUILD_TESTBENCH "Build testbench of YYCCommonplace." OFF) option(YYCC_BUILD_DOC "Build document of YYCCommonplace." OFF) -# Detect MSVC IDE environment. -# If we in it, we should add configuration and build type in install path. -if (CMAKE_GENERATOR MATCHES "Visual Studio") - # Do Visual Studio specific - set(YYCC_INSTALL_PATH_LIB lib/${CMAKE_VS_PLATFORM_NAME}/$) - set(YYCC_INSTALL_PATH_BIN bin/${CMAKE_VS_PLATFORM_NAME}) -else() - # Other stuff - set(YYCC_INSTALL_PATH_LIB lib) - set(YYCC_INSTALL_PATH_BIN bin) -endif() +# Setup install path from CMake provided install path for convenient use. +include(GNUInstallDirs) +set(YYCC_INSTALL_INCLUDE_PATH ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH +"Public header install path relative to CMAKE_INSTALL_PREFIX unless set to an absolute path.") +set(YYCC_INSTALL_LIB_PATH ${CMAKE_INSTALL_LIBDIR} CACHE PATH +"Library install path relative to CMAKE_INSTALL_PREFIX unless set to an absolute path.") +set(YYCC_INSTALL_BIN_PATH ${CMAKE_INSTALL_BINDIR} CACHE PATH +"Binary install path relative to CMAKE_INSTALL_PREFIX unless set to an absolute path.") +set(YYCC_INSTALL_DOC_PATH ${CMAKE_INSTALL_DOCDIR} CACHE PATH +"Non-arch doc install path relative to CMAKE_INSTALL_PREFIX unless set to an absolute path.") -# Import 2 build targets +# Import 3 build targets add_subdirectory(src) if (YYCC_BUILD_TESTBENCH) add_subdirectory(testbench) @@ -29,12 +28,12 @@ if (YYCC_BUILD_DOC) add_subdirectory(doc) endif () -# Install project package infos -# Package target +# Install target and package +# Install target install(EXPORT YYCCommonplaceTargets FILE YYCCommonplaceTargets.cmake NAMESPACE YYCC:: - DESTINATION lib/cmake/YYCCommonplace + DESTINATION ${YYCC_INSTALL_LIB_PATH}/cmake/YYCCommonplace ) # Package configuration file include(CMakePackageConfigHelpers) @@ -46,14 +45,14 @@ write_basic_package_version_file( configure_package_config_file( ${CMAKE_CURRENT_LIST_DIR}/cmake/YYCCommonplaceConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/YYCCommonplaceConfig.cmake" - INSTALL_DESTINATION lib/cmake/YYCCommonplace + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/YYCCommonplace ) -# Copy to install destination +# Copy package files to install destination install( FILES "${CMAKE_CURRENT_BINARY_DIR}/YYCCommonplaceConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/YYCCommonplaceConfigVersion.cmake" DESTINATION - lib/cmake/YYCCommonplace + ${CMAKE_INSTALL_LIBDIR}/cmake/YYCCommonplace ) diff --git a/cmake/YYCCommonplaceConfig.cmake.in b/cmake/YYCCommonplaceConfig.cmake.in index e351d9a..8add9bb 100644 --- a/cmake/YYCCommonplaceConfig.cmake.in +++ b/cmake/YYCCommonplaceConfig.cmake.in @@ -1,2 +1,7 @@ -# Add the targets file -include("${CMAKE_CURRENT_LIST_DIR}/YYCCommonplaceTargets.cmake") \ No newline at end of file + +@PACKAGE_INIT@ + +# Include targets file +include("${CMAKE_CURRENT_LIST_DIR}/YYCCommonplaceTargets.cmake") + +check_required_components(YYCCommonplace) \ No newline at end of file diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt index 2c6c1a4..3804460 100644 --- a/doc/CMakeLists.txt +++ b/doc/CMakeLists.txt @@ -14,5 +14,5 @@ add_custom_target (YYCCDocumentation # Install built documentation install (DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html - DESTINATION doc + DESTINATION ${YYCC_INSTALL_DOC_PATH} ) diff --git a/script/build.bat b/script/win_build.bat similarity index 51% rename from script/build.bat rename to script/win_build.bat index 59cc1e0..c70905e 100644 --- a/script/build.bat +++ b/script/win_build.bat @@ -7,37 +7,48 @@ IF EXIST %README_PATH% ( EXIT /b ) -:: Create essential folder +:: Create main binary directory MKDIR bin CD bin +:: Create build folder MKDIR Win32 MKDIR x64 MKDIR documentation +:: Create install folder MKDIR install +CD install +MKDIR Win32_Debug +MKDIR Win32_Release +MKDIR x64_Debug +MKDIR x64_Release +CD .. :: Build for Win32 CD Win32 cmake -G "Visual Studio 16 2019" -A Win32 -DYYCC_BUILD_TESTBENCH=ON ../.. cmake --build . --config Debug -cmake --install . --prefix=../install --config Debug +cmake --install . --prefix=../install/Win32_Debug --config Debug cmake --build . --config Release -cmake --install . --prefix=../install --config Release +cmake --install . --prefix=../install/Win32_Release --config Release CD .. :: Build for x64 CD x64 cmake -G "Visual Studio 16 2019" -A x64 -DYYCC_BUILD_TESTBENCH=ON ../.. cmake --build . --config Debug -cmake --install . --prefix=../install --config Debug +cmake --install . --prefix=../install/x64_Debug --config Debug cmake --build . --config Release -cmake --install . --prefix=../install --config Release +cmake --install . --prefix=../install/x64_Release --config Release CD .. :: Build for documentation CD documentation -cmake -DYYCC_BUILD_DOC=ON ../.. +cmake -G "Visual Studio 16 2019" -A x64 -DYYCC_BUILD_DOC=ON ../.. +cmake --build . --config Release cmake --build . --target YYCCDocumentation -cmake --install . --prefix=../install +cmake --install . --prefix=../install/x64_Release --config Release CD .. -ECHO DONE +:: Exit to original path +CD .. +ECHO Windows CMake Build Done diff --git a/script/win_msvc_build.bat b/script/win_msvc_build.bat new file mode 100644 index 0000000..8dfbc01 --- /dev/null +++ b/script/win_msvc_build.bat @@ -0,0 +1,50 @@ +@ECHO OFF +SET README_PATH=%CD%\README.md +IF EXIST %README_PATH% ( + REM DO NOTHING +) ELSE ( + ECHO Error: You must run this script at the root folder of this project! + EXIT /b +) + +:: Enter main binary directory +CD bin + +:: Create MSVC binary directory +MKDIR msvc_install +CD msvc_install +:: Create direcotries tree +MKDIR bin +MKDIR include +MKDIR lib +MKDIR share +CD bin +MKDIR Win32 +MKDIR x64 +CD .. +CD lib +MKDIR Win32\Debug +MKDIR Win32\Release +MKDIR x64\Debug +MKDIR x64\Release +CD .. +:: Exit MSVC binary directory +CD .. + +:: Copy result +:: Copy include from x64_Release build +XCOPY install\x64_Release\include msvc_install\include\ /E /Y +:: Copy document from x64_Release build +XCOPY install\x64_Release\share msvc_install\share\ /E /Y +:: Copy binary testbench +COPY install\Win32_Release\bin\YYCCTestbench.exe msvc_install\bin\Win32\YYCCTestbench.exe /Y +COPY install\x64_Release\bin\YYCCTestbench.exe msvc_install\bin\x64\YYCCTestbench.exe /Y +:: Copy static library +COPY install\Win32_Debug\lib\YYCCommonplace.lib msvc_install\lib\Win32\Debug\YYCCommonplace.lib /Y +COPY install\Win32_Release\lib\YYCCommonplace.lib msvc_install\lib\Win32\Release\YYCCommonplace.lib /Y +COPY install\x64_Debug\lib\YYCCommonplace.lib msvc_install\lib\x64\Debug\YYCCommonplace.lib /Y +COPY install\x64_Release\lib\YYCCommonplace.lib msvc_install\lib\x64\Release\YYCCommonplace.lib /Y + +:: Exit to original path +CD .. +ECHO Windows MSVC Build Done diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 79b89d8..6b68e6b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -74,8 +74,8 @@ PRIVATE # Install binary and headers install(TARGETS YYCCommonplace EXPORT YYCCommonplaceTargets - LIBRARY DESTINATION ${YYCC_INSTALL_PATH_LIB} - ARCHIVE DESTINATION ${YYCC_INSTALL_PATH_LIB} - INCLUDES DESTINATION include - FILE_SET HEADERS DESTINATION include + LIBRARY DESTINATION ${YYCC_INSTALL_LIB_PATH} + ARCHIVE DESTINATION ${YYCC_INSTALL_LIB_PATH} + INCLUDES DESTINATION ${YYCC_INSTALL_INCLUDE_PATH} + FILE_SET HEADERS DESTINATION ${YYCC_INSTALL_INCLUDE_PATH} ) diff --git a/testbench/CMakeLists.txt b/testbench/CMakeLists.txt index e496f87..db1135f 100644 --- a/testbench/CMakeLists.txt +++ b/testbench/CMakeLists.txt @@ -35,7 +35,6 @@ PRIVATE # Install testbench only on Release mode install(TARGETS YYCCTestbench - EXPORT YYCCTestbenchTargets CONFIGURATIONS Release - RUNTIME DESTINATION ${YYCC_INSTALL_PATH_BIN} + RUNTIME DESTINATION ${YYCC_INSTALL_BIN_PATH} )