From b1cfe33b7c168584a52957f04f85ca9330d917da Mon Sep 17 00:00:00 2001 From: zjyhjqs Date: Thu, 12 Dec 2024 00:16:34 +0800 Subject: [PATCH 1/8] fix: use the official pre-defined macro for testing windows see: https://github.com/cpredef/predef/blob/master/OperatingSystems.md#windows --- include/gmssl/dylib.h | 2 +- include/gmssl/socket.h | 2 +- src/file.c | 2 +- src/sgd.h | 14 +++++++------- src/socket.c | 2 +- src/tls.c | 4 ++-- tools/tlcp_client.c | 8 ++++---- tools/tls12_client.c | 7 +++---- tools/tls13_client.c | 9 ++++----- 9 files changed, 24 insertions(+), 26 deletions(-) diff --git a/include/gmssl/dylib.h b/include/gmssl/dylib.h index c2aefaa82..3435e8809 100644 --- a/include/gmssl/dylib.h +++ b/include/gmssl/dylib.h @@ -19,7 +19,7 @@ extern "C" { #endif -#ifdef WIN32 +#ifdef _WIN32 #include diff --git a/include/gmssl/socket.h b/include/gmssl/socket.h index 926d22b7b..0354fac1a 100644 --- a/include/gmssl/socket.h +++ b/include/gmssl/socket.h @@ -21,7 +21,7 @@ extern "C" { -#ifdef WIN32 +#ifdef _WIN32 #pragma comment (lib, "Ws2_32.lib") #pragma comment (lib, "Mswsock.lib") #pragma comment (lib, "AdvApi32.lib") diff --git a/src/file.c b/src/file.c index 149724ccb..78fdbc555 100644 --- a/src/file.c +++ b/src/file.c @@ -21,7 +21,7 @@ int file_size(FILE *fp, size_t *size) int fd; struct stat st; -#ifdef WIN32 +#ifdef _WIN32 fd = _fileno(fp); #else fd = fileno(fp); diff --git a/src/sgd.h b/src/sgd.h index 6656178e0..bd747c607 100644 --- a/src/sgd.h +++ b/src/sgd.h @@ -56,14 +56,14 @@ #define SGD_ZUC_EIA3 (SGD_ZUC|SGD_EIA3) /* public key usage */ -#define SGD_PK_SIGN 0x0100 // FIXME: correct? -#define SGD_PK_DH 0x0200 // FIXME: correct? -#define SGD_PK_ENC 0x0400 // FIXME: correct? +#define SGD_PK_SIGN 0x0100 // FIXME: correct? +#define SGD_PK_DH 0x0200 // FIXME: correct? +#define SGD_PK_ENC 0x0400 // FIXME: correct? /* public key types */ #define SGD_RSA 0x00010000 -#define SGD_RSA_SIGN (SGD_RSA|SGD_PK_SIGN) // FIXME: correct? -#define SGD_RSA_ENC (SGD_RSA|SGD_PK_ENC) // FIXME: correct? +#define SGD_RSA_SIGN (SGD_RSA|SGD_PK_SIGN) // FIXME: correct? +#define SGD_RSA_ENC (SGD_RSA|SGD_PK_ENC) // FIXME: correct? #define SGD_SM2 0x00020100 #define SGD_SM2_1 0x00020200 // SM2 Signature Scheme #define SGD_SM2_2 0x00020400 // SM2 Key Exchange Protocol @@ -275,7 +275,7 @@ typedef int32_t SGD_BOOL; #define SGD_STATUS_READY 0x00000202 #define SGD_STATUS_EXCEPTION 0x00000203 -#ifdef WIN32 +#ifdef _WIN32 #include #else typedef signed char INT8; @@ -318,7 +318,7 @@ typedef HANDLE HCONTAINER; #define TRUE 0x00000001 #endif -#ifdef WIN32 +#ifdef _WIN32 #define DEVAPI __stdcall #else #define DEVAPI diff --git a/src/socket.c b/src/socket.c index e785a9265..ff5e4d576 100644 --- a/src/socket.c +++ b/src/socket.c @@ -16,7 +16,7 @@ #include -#ifdef WIN32 +#ifdef _WIN32 int tls_socket_lib_init(void) { WORD wVersion = MAKEWORD(2, 2); diff --git a/src/tls.c b/src/tls.c index d89dc1eee..ba7fe9270 100644 --- a/src/tls.c +++ b/src/tls.c @@ -2320,8 +2320,8 @@ void tls_cleanup(TLS_CONNECT *conn) int tls_set_socket(TLS_CONNECT *conn, tls_socket_t sock) { int flags = 0; -#ifdef WIN32 - if( ioctlsocket(sock, FIONBIO, &flags) != 0) { +#ifdef _WIN32 + if( ioctlsocket(sock, FIONBIO, &flags) != 0) { error_puts("socket in non-blocking mode"); //nginx will pass a socket in non-blocking mode //return -1; diff --git a/tools/tlcp_client.c b/tools/tlcp_client.c index 873774351..91b3b0da8 100644 --- a/tools/tlcp_client.c +++ b/tools/tlcp_client.c @@ -256,7 +256,7 @@ int tlcp_client_main(int argc, char *argv[]) FD_SET(conn.sock, &fds); if (read_stdin) { -#ifdef WIN32 +#ifdef _WIN32 if (fgets(buf, sizeof(buf), stdin)) { if (tls_send(&conn, (uint8_t *)buf, strlen(buf), &len) != 1) { fprintf(stderr, "%s: send error\n", prog); @@ -266,9 +266,9 @@ int tlcp_client_main(int argc, char *argv[]) if (!feof(stdin)) { fprintf(stderr, "%s: length of input line exceeds buffer size\n", prog); goto end; - } + } read_stdin = 0; - } + } #else FD_SET(STDIN_FILENO, &fds); // in POSIX, first arg type is int #endif @@ -278,7 +278,7 @@ int tlcp_client_main(int argc, char *argv[]) goto end; } -#ifdef WIN32 +#ifdef _WIN32 #else if (read_stdin && FD_ISSET(STDIN_FILENO, &fds)) { diff --git a/tools/tls12_client.c b/tools/tls12_client.c index 5041dea27..2983ab218 100644 --- a/tools/tls12_client.c +++ b/tools/tls12_client.c @@ -159,7 +159,7 @@ int tls12_client_main(int argc, char *argv[]) FD_ZERO(&fds); FD_SET(conn.sock, &fds); -#ifdef WIN32 +#ifdef _WIN32 #else FD_SET(fileno(stdin), &fds); #endif @@ -178,14 +178,13 @@ int tls12_client_main(int argc, char *argv[]) fwrite(buf, 1, len, stdout); fflush(stdout); - // FIXME: change `tls_recv` API or functions + // FIXME: change `tls_recv` API or functions if (conn.datalen == 0) { break; } } - } -#ifdef WIN32 +#ifdef _WIN32 #else if (FD_ISSET(fileno(stdin), &fds)) { memset(send_buf, 0, sizeof(send_buf)); diff --git a/tools/tls13_client.c b/tools/tls13_client.c index ee94ed79d..4150138eb 100644 --- a/tools/tls13_client.c +++ b/tools/tls13_client.c @@ -93,7 +93,7 @@ int tls13_client_main(int argc, char *argv[]) return -1; } if (!(hp = gethostbyname(host))) { - //herror("tls13_client: '-host' invalid"); + //herror("tls13_client: '-host' invalid"); goto end; } @@ -157,7 +157,7 @@ int tls13_client_main(int argc, char *argv[]) FD_ZERO(&fds); FD_SET(conn.sock, &fds); -#ifdef WIN32 +#ifdef _WIN32 #else FD_SET(fileno(stdin), &fds); #endif @@ -177,14 +177,13 @@ int tls13_client_main(int argc, char *argv[]) fwrite(buf, 1, len, stdout); fflush(stdout); - // FIXME: change tls13_recv API + // FIXME: change tls13_recv API if (conn.datalen == 0) { break; } } - } -#ifdef WIN32 +#ifdef _WIN32 #else if (FD_ISSET(fileno(stdin), &fds)) { memset(send_buf, 0, sizeof(send_buf)); From 53f9d90fb0cceceac3e36d775ce357e6428f85d8 Mon Sep 17 00:00:00 2001 From: zjyhjqs Date: Sat, 14 Dec 2024 16:30:48 +0800 Subject: [PATCH 2/8] feat: add cmake config support, and simplify the modification of project version (only the `version.h`) --- CMakeLists.txt | 70 ++++++++++++++++++++++++++++++-------- cmake/GmSSLConfig.cmake.in | 7 ++++ include/gmssl/version.h | 6 ++-- 3 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 cmake/GmSSLConfig.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d7d12d92..5feb4a30c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,16 +3,22 @@ if (CMAKE_VERSION VERSION_LESS "3.0") else() cmake_minimum_required(VERSION 3.6) endif() -project(GmSSL C) -SET(CMAKE_PROJECT_HOMEPAGE_URL "http://gmssl.org") +file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/gmssl/version.h" _gmssl_version_h_contents REGEX "#define GMSSL_VERSION_STR") +string(REGEX MATCH "\"GmSSL [^\"]*" _gmssl_version ${_gmssl_version_h_contents}) +string(REGEX REPLACE ".* ([0-9]+\\.[0-9]+\\.[0-9]+) (.*)" "\\1-\\2" _gmssl_version ${_gmssl_version}) +unset(_gmssl_version_h_contents) + +string(REGEX REPLACE "([0-9]+\.[0-9]+\.[0-9]+).+" "\\1" _gmssl_version_sem "${_gmssl_version}") +project(GmSSL + VERSION ${_gmssl_version_sem} + HOMEPAGE_URL "http://gmssl.org" + LANGUAGES C) set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) SET(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake") -include_directories(include) - if(MSVC) #add_compile_options(/O2) # conflict with CI /0d (not optimized) else() @@ -518,7 +524,7 @@ if (ENABLE_SKF) src/skf/skf_wisec.c) list(APPEND tools tools/skfutil.c) add_library(skf_dummy SHARED src/skf/skf_dummy.c) - set_target_properties(skf_dummy PROPERTIES VERSION 3.1 SOVERSION 3) + set_target_properties(skf_dummy PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR}) endif() if (ENABLE_SDF) @@ -549,8 +555,12 @@ if (CMAKE_C_COMPILER_ID MATCHES "MSVC") endif() add_library(gmssl ${src}) +add_library(GmSSL::${PROJECT_NAME} ALIAS gmssl) - +target_include_directories(gmssl PUBLIC + $ + $ +) @@ -577,11 +587,41 @@ endif() set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") -SET_TARGET_PROPERTIES(gmssl PROPERTIES VERSION 3.1 SOVERSION 3) - - -install(TARGETS gmssl ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin) -install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/gmssl DESTINATION include) +include(GNUInstallDirs) +set(GmSSL_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) + +set(_targets_export_name ${PROJECT_NAME}Targets) +include(CMakePackageConfigHelpers) +configure_package_config_file( + ${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in + ${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake + INSTALL_DESTINATION ${GmSSL_CMAKE_DIR}) +write_basic_package_version_file( + ${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion) +install( + FILES + ${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake + ${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake + DESTINATION ${GmSSL_CMAKE_DIR}) + +install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/gmssl DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +install(TARGETS gmssl + EXPORT ${_targets_export_name} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +set_target_properties(gmssl PROPERTIES + VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} + EXPORT_NAME ${PROJECT_NAME}) +export(EXPORT ${_targets_export_name} + NAMESPACE GmSSL:: + FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/${_targets_export_name}.cmake") + +install(EXPORT ${_targets_export_name} + NAMESPACE GmSSL:: + DESTINATION ${GmSSL_CMAKE_DIR}) if(UNIX AND NOT APPLE) message(STATUS "Detected Linux, configuring /etc/ld.so.conf.d/gmssl.conf") @@ -642,9 +682,11 @@ endif() # Install the STGZ package # ./GmSSL--Linux.sh --prefix=/usr/local # -set(CPACK_PACKAGE_NAME "GmSSL") +# The `-DEV` part is important +string(REGEX REPLACE "([0-9]+\.[0-9]+)\.([0-9]+.*)" "\\2" CPACK_PACKAGE_VERSION_PATCH "${_gmssl_version}") set(CPACK_PACKAGE_VENDOR "GmSSL develop team") -set(CPACK_PACKAGE_VERSION "3.1.2-Dev") -set(CPACK_PACKAGE_DESCRIPTION_FILE ${PROJECT_SOURCE_DIR}/README.md) +set(CPACK_RESOURCE_FILE_LICENSE ${PROJECT_SOURCE_DIR}/LICENSE) +set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md) +set(CPACK_GENERATOR TGZ) # default generator, can be overwritten by `cpack -G` set(CPACK_NSIS_MODIFY_PATH ON) include(CPack) diff --git a/cmake/GmSSLConfig.cmake.in b/cmake/GmSSLConfig.cmake.in new file mode 100644 index 000000000..cf0a1690a --- /dev/null +++ b/cmake/GmSSLConfig.cmake.in @@ -0,0 +1,7 @@ +@PACKAGE_INIT@ + +if (NOT TARGET GmSSL::GmSSL) + include(${CMAKE_CURRENT_LIST_DIR}/@_targets_export_name@.cmake) +endif () + +check_required_components(GmSSL) diff --git a/include/gmssl/version.h b/include/gmssl/version.h index c516bd8b9..6e84c8959 100644 --- a/include/gmssl/version.h +++ b/include/gmssl/version.h @@ -16,10 +16,8 @@ extern "C" { #endif - -// Also update CPACK_PACKAGE_VERSION in CMakeLists.txt -#define GMSSL_VERSION_NUM 30102 -#define GMSSL_VERSION_STR "GmSSL 3.1.2 Dev" +#define GMSSL_VERSION_NUM 30102 +#define GMSSL_VERSION_STR "GmSSL 3.1.2 Dev" int gmssl_version_num(void); const char *gmssl_version_str(void); From a0e803c6c6be4bda48dd0da15ee7e1addc8253ae Mon Sep 17 00:00:00 2001 From: zjyhjqs Date: Sat, 14 Dec 2024 17:46:25 +0800 Subject: [PATCH 3/8] fix: default value of `CMAKE_INSTALL_PREFIX` should not be set (default to `c:/Program Files/${PROJECT_NAME}` on Windows by cmake) see: https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5feb4a30c..069c5d896 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -653,8 +653,7 @@ endif() if (CMAKE_C_COMPILER_ID MATCHES "MSVC") set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT gmssl-bin) - set(CMAKE_INSTALL_PREFIX "C:/Program Files/GmSSL") # change by `cmake -DCMAKE_INSTALL_PREFIX=C:\path\to\install` - # run `set path=%path%;C:\Program Files\GmSSL\bin` + add_compile_definitions(_CRT_SECURE_NO_WARNINGS) target_compile_options(gmssl PRIVATE /utf-8) From fe07828c2ba02fea9daed58b414e814d1fb90c76 Mon Sep 17 00:00:00 2001 From: zjyhjqs Date: Sat, 14 Dec 2024 17:50:36 +0800 Subject: [PATCH 4/8] fix: use `MSVC` to check all MSVC front-end compiler (including clang-cl) --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 069c5d896..bc5608fea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -549,7 +549,7 @@ endif() option(BUILD_SHARED_LIBS "Build using shared libraries" ON) -if (CMAKE_C_COMPILER_ID MATCHES "MSVC") +if (MSVC) message(STATUS "CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS") set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) # set before add_library endif() @@ -651,7 +651,7 @@ if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "iOS") install(TARGETS gmssl-bin RUNTIME DESTINATION bin) endif() -if (CMAKE_C_COMPILER_ID MATCHES "MSVC") +if (MSVC) set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT gmssl-bin) add_compile_definitions(_CRT_SECURE_NO_WARNINGS) From e2f4b44a6ccd688e8bf8ac2a39d5c0f6f3e69690 Mon Sep 17 00:00:00 2001 From: zjyhjqs Date: Sat, 14 Dec 2024 18:09:46 +0800 Subject: [PATCH 5/8] fix: delete the explicit `-O3` optimization flags, because it's unnecessary for `Release` mode and incorrect for other modes, e.g. `Debug` and `RelWithDebInfo` Note: The default release optimization flags is `/O2` on `MSVC` frontend and `-O3` on `GNU` frontend, so it's no need to set it. see: `Modules/Compiler/GNU.cmake` --- CMakeLists.txt | 6 ------ 1 file changed, 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bc5608fea..91c4b1104 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,12 +19,6 @@ set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) SET(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake") -if(MSVC) - #add_compile_options(/O2) # conflict with CI /0d (not optimized) -else() - add_compile_options(-O3) -endif() - option(ENABLE_TEST_SPEED "Enable test speed" OFF) From 7a955e61c1e6ebba54f1ac90b843eee2327c90a0 Mon Sep 17 00:00:00 2001 From: zjyhjqs Date: Sat, 14 Dec 2024 18:34:33 +0800 Subject: [PATCH 6/8] perf: use modernized `compile_options` --- CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 91c4b1104..817398423 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -318,7 +318,7 @@ if (ENABLE_SM4_AVX2) list(FIND src src/sm4.c sm4_index) list(REMOVE_AT src ${sm4_index}) list(INSERT src ${sm4_index} src/sm4_avx2.c) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native") + add_compile_options("-march=native") endif() if (ENABLE_SM4_AESNI) @@ -326,7 +326,7 @@ if (ENABLE_SM4_AESNI) list(FIND src src/sm4.c sm4_index) list(REMOVE_AT src ${sm4_index}) list(INSERT src ${sm4_index} src/sm4_aesni.c) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native") + add_compile_options("-march=native") endif() @@ -346,7 +346,7 @@ if (ENABLE_SM4_CTR_AESNI_AVX) list(FIND src src/sm4_ctr.c sm4_ctr_index) list(REMOVE_AT src ${sm4_ctr_index}) list(INSERT src ${sm4_ctr_index} src/sm4_ctr_aesni_avx.c) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native") + add_compile_options("-march=native") endif() @@ -469,7 +469,7 @@ if (ENABLE_INTEL_RDRAND) message(STATUS "ENABLE_INTEL_RDRAND") add_definitions(-DENABLE_INTEL_RDRAND) list(APPEND src src/rdrand.c) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mrdrnd") + add_compile_options("-mrdrnd") endif() if (ENABLE_INTEL_RDSEED) set(CMAKE_REQUIRED_FLAGS "-rdseed") @@ -479,7 +479,7 @@ if (ENABLE_INTEL_RDRAND) if (HAVE_INTEL_RDSEED) message(STATUS "ENABLE_INTEL_RDSEED") add_definitions(-DENABLE_INTEL_RDSEED) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mrdseed") + add_compile_options("-mrdseed") endif() endif() endif() From 8576920c19ba20bb91790772e28ef72385b5f495 Mon Sep 17 00:00:00 2001 From: zjyhjqs Date: Sat, 14 Dec 2024 19:30:56 +0800 Subject: [PATCH 7/8] fix: wrong library name --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 817398423..324c0e3f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -559,7 +559,7 @@ target_include_directories(gmssl PUBLIC if (WIN32) - target_link_libraries(gmssl -lws2_32) + target_link_libraries(gmssl PRIVATE ws2_32) elseif (APPLE) if (ENABLE_SDF) target_link_libraries(gmssl dl) From 8e35fcc1dfdee7cb3cf541489ad01a2fab51f7d3 Mon Sep 17 00:00:00 2001 From: zjyhjqs Date: Sun, 15 Dec 2024 13:35:41 +0800 Subject: [PATCH 8/8] doc: update documentation about compiling and using the library --- INSTALL.md | 103 +++++++++++++++++++++++++++-------------------------- README.md | 27 +++++--------- 2 files changed, 61 insertions(+), 69 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index d4fa16aca..2c3b7be76 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -6,22 +6,20 @@ GmSSL当前版本采用CMake构建系统。由于CMake是一个跨平台的编译、安装工具,因此GmSSL可以在大多数主流操作系统上编译、安装和运行。GmSSL项目官方测试了Windows (包括Visual Stduio和Cygwin)、Linux、Mac、Android和iOS这几个主流操作系统上的编译,并通过GitHub的CI工作流对提交的最新代码进行自动化的编译测试。 -和其他基于CMake的开源项目类似,GmSSL的构建过程主要包含配置、编译、测试、安装这几个步骤。以Linux操作系统环境为例,在下载并解压GmSSL源代码后,进入源代码目录,执行如下命令: +和其他基于CMake的开源项目类似,GmSSL的构建过程主要包含配置、编译、测试、安装这几个步骤。在下载并解压GmSSL源代码后,进入源代码目录,执行如下命令: ```bash -mkdir build -cd build -cmake .. -make -make test -sudo make install +cmake -B build # -G "Ninja Multi-Config" +cmake --build build +ctest --test-dir build +sudo cmake --install build # --prefix /path ``` 就可以完成配置、编译、测试和安装。 -在执行`make`编译成功后,在`build/bin`目录下会生成项目的可执行文件和库文件。对于密码工具来说,在安装使用之前通过`make test`进行测试是重要的一步,如果测试失败,那么不应该使用这个软件。在发生某个测试错误后,可以执行`build/bin`下的具体某个测试命令行,如`sm4test`,这样可以看到具体的错误打印信息。 +在执行`cmake --build`编译成功后,在`build/bin`目录下会生成项目的可执行文件和库文件。对于密码工具来说,在安装使用之前通过`ctest`进行测试是重要的一步,如果测试失败,那么不应该使用这个软件。在发生某个测试错误后,可以执行`build/bin`下的具体某个测试命令行,如`sm4test`,这样可以看到具体的错误打印信息。 -执行`sudo make install`,安装完成后,可以命令行中调用`gmssl`命令行工具。在Linux和Mac环境下,头文件通常被安装在`/usr/local/include/gmssl`目录下,库文件被安装在`/usr/local/lib`目录下。 +执行`sudo cmake --install`,安装完成后,可以命令行中调用`gmssl`命令行工具。在Linux和Mac环境下,头文件通常被安装在`/usr/local/include/gmssl`目录下,库文件被安装在`/usr/local/lib`目录下。 ## 项目源代码 @@ -82,7 +80,7 @@ GmSSL包含了针对特定硬件和处理指令集的密码算法优化实现, ```bash cmake .. -DENABLE_BROKEN_CRYPTO=ON -make +cmake --build . ``` 重新编译后,加入GmSSL库文件的算法包括: @@ -96,28 +94,26 @@ make CMake支持通过指定不同的构建系统生成器(Generator),生成不同类型的Makefile。在Windows和Visual Studio环境下,CMake即可以生成常规的Visual Studio解决方案(.sln)文件,在Visual Studio图形界面中完成编译,也可以生成类似于Linux环境下的Makefile文件,在命令行环境下完成编译和测试。 -### 生成Makefile编译 +### 生成Makefile/Ninja编译 在安装完Visual Studio之后,在启动菜单栏中会出现Visual Studio菜单目录,其中包含x64 Native Tools Command Prompt for VS 2022等多个终端命令行环境菜单项。 -```bash +```pwsh C:\Program Files\Microsoft Visual Studio\2022\Community>cd /path/to/gmssl -mkdir build -cd build -cmake .. -G "NMake Makefiles" -nmake -nmake test +cmake -B build -G "NMake Makefiles" +# cmake -B build -G "Ninja" +ctest --test-dir build ``` 在编译完成后直接执行安装会报权限错误,这是因为安装过程需要向系统目录中写入文件,而当前打开命令行环境的用户不具备该权限。可以通过右键选择“更多-以管理员身份运行”打开x64 Native Tools Command Prompt for VS 2022终端,执行 -``` -nmake install +```pwsh +cmake --install build ``` 那么`gmssl`命令行程序、头文件和库文件分别被写入`C:/Program Files/GmSSL/bin`、`C:/Program Files/GmSSL/include`、`C:/Program Files/GmSSL/lib`这几个系统目录中。为了能够直接在命令行环境任意目录下执行`gmssl`命令行程序,需要将其安装目录加入到系统路径中,可以执行: -```bash +```batch set path=%path%;C:\Program Files\GmSSL\bin ``` @@ -127,11 +123,10 @@ set path=%path%;C:\Program Files\GmSSL\bin 在安装完Visual Studio之后,在启动菜单栏中会出现Visual Studio菜单目录,其中包含x64 Native Tools Command Prompt for VS 2022等多个终端命令行环境菜单项。 -```bash +```pwsh C:\Program Files\Microsoft Visual Studio\2022\Community>cd /path/to/gmssl -mkdir build -cd build -cmake .. +# 此处`-G`可以不显式指定, 因为cmake在windows上使用MSBuild作为默认generator +cmake -B build # -G "Visual Studio 17 2022" ``` 完成后可以看到CMake在`build`目录下生成了一个`GmSSL.sln`文件和大量的`.vcxproj`文件。 @@ -179,15 +174,13 @@ cp "/cygdrive/c/Users/Guan Zhi/Downloads/GmSSL-master.zip" ~/ ```bash unzip GmSSL-master.zip cd GmSSL-master -mkdir build -cd build -cmake .. -make -make test -make install +cmake -B build +cmake --build build +ctest --test-dir build +cmake --install build # --prefix /path ``` -注意,由于在Cygwin环境中用户本身具有系统权限,因此在执行`make install`时不需要`sudo`。 +注意,由于在Cygwin环境中用户本身具有系统权限,因此在执行`cmake --install`时不需要`sudo`。 在安装完成之后,可以在Cygwin的命令行环境下执行`gmssl`命令行,或者运行源代码`demo`目录下的演示脚本。 @@ -218,7 +211,7 @@ cmake --build . --config Release ```bash mkdir build; cd build cmake .. -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-23 -make +cmake --build . ``` ## 安装包构建 @@ -258,23 +251,33 @@ make package 在正式发布之前,需要在测试平台上编译、测试、安装。验证`gmssl`命令行可以正确使用,验证`sm3_demo.c`可以正确和`-lgmssl`编译,并且可以正确输出哈希值。 -完成编译和测试后,在`build`目录下执行如下操作 - -``` bash -#!/bin/bash -x -VERSION=3.2.0 -OS=macos -ARCH=arm64 -mkdir build; cd build; cmake ..; make -cmake .. -DBUILD_SHARED_LIBS=OFF; make -mkdir gmssl-$VERSION -cd gmssl-$VERSION -mkdir bin; mkdir lib; mkdir include -cp ../bin/gmssl bin -cp -P ../bin/libgmssl* lib -cp -r ../../include/gmssl include -cd .. -tar czvf gmssl-$VERSION-$OS-$ARCH.tar.gz gmssl-$VERSION +完成编译和测试后,进行编译打包操作。 + +### 生成开发压缩包 + +对于需要打包多模式 (`Debug`, `Release`, `RelWithDebInfo`) 的 (主要为Windows平台,因为`MSVC`的`Debug`/`Release`标准库运行时独立),可以根据以下示例进行操作。示例中脚本需要环境中存在ninja。 + +```shell +cmake -B build -G "Ninja Multi-Config" -DCMAKE_CONFIGURATION_TYPES="Debug;Release" -DBUILD_SHARED_LIBS=OFF -DCMAKE_DEBUG_POSTFIX="d" +cd build +cmake --build . --config Debug +cmake --build . --config Release +cpack -C "Debug;Release" # -G TGZ ``` -其中`cmake .. -DBUILD_SHARED_LIBS=OFF; make`重新生成了静态库,以及和静态库连接的`gmssl`二进制程序,因此最终打包的`gmssl`命令行不依赖系统库之外的动态库。 +其中指定`BUILD_SHARED_LIBS=OFF`重新生成了静态库,以及和静态库连接的`gmssl`二进制程序,因此最终打包的`gmssl`命令行不依赖系统库之外的动态库。 + +### 在CMake中进行依赖 + +GmSSL库较为简单,只需将对应头文件路径与所需链接库配置进target即可。但目前也已支持了Modern CMake的依赖方式。 + +假设我们将以上打包内容解压至`/path/GmSSL-3.1.2-win64` + +```cmake +set(GmSSL_ROOT "/path/GmSSL-3.1.2-win64") +find_package(GmSSL REQUIRED) # required表示必须找到,否则报错 + +# `MyTarget` is created by `add_library` or `add_executable` +target_link_libraries(MyTarget GmSSL::GmSSL) +``` +通过以上配置,即可正常使用GmSSL。 diff --git a/README.md b/README.md index 3afe399a9..84d51083a 100644 --- a/README.md +++ b/README.md @@ -23,27 +23,16 @@ GmSSL是由北京大学自主开发的国产商用密码开源库,实现了对 GmSSL 3 采用了cmake构建系统。下载源代码后将其解压缩,进入源码目录,执行: -```bash -mkdir build -cd build -cmake .. -make -make test -sudo make install +```shell +cmake -B build # -G "Ninja Multi-Config" +cmake --build build +ctest --test-dir build +sudo cmake --install build # --prefix /path ``` -在`make install`完成后,GmSSL会在默认安装目录中安装`gmssl`命令行工具,在头文件目录中创建`gmssl`目录,并且在库目录中安装`libgmssl.a`、`libgmssl.so`等库文件。 +可以手动指定generator,比如`cmake -B build -G Ninja`以指定Ninja作为generator。 -### Visual Studio环境编译 - -在Visual Studio命令提示符下执行: - -```bash -mkdir build -cd build -cmake .. -G "NMake Makefiles" -DWIN32=ON -nmake -``` +在`cmake --install`完成后,GmSSL会在默认安装目录中安装`gmssl`命令行工具,在头文件目录中创建`gmssl`目录,并且在库目录中安装`libgmssl.a`、`libgmssl.so`等库文件。 ## 主要功能 @@ -97,7 +86,7 @@ GmSSL 3.0版本重写了所有的代码并改变了原有API,因此当前GmSSL ``` cmake .. -DENABLE_TEST_SPEED=ON -make +cmake --build . ./bin/sm4test; ./bin/sm3test; ./bin/sm2_signtest; ./bin/sm2_enctest; ./bin/sm9test; ./bin/zuctest ```