From bee49a9ea4a494b0f7a081ba018384ab1b94a6ae Mon Sep 17 00:00:00 2001 From: Juuso Alasuutari Date: Fri, 10 Jan 2025 14:20:41 +0200 Subject: [PATCH 1/3] [C][Client] Generate CMakeLists.txt for all C sample clients The sample client defined in bin/configs/c.yaml has CMakeLists.txt in its .openapi-generator-ignore. It's is the only C client that doesn't (re-)generate CMakeLists.txt, the remaining two seem fine regardless. --- .../petstore/c/.openapi-generator-ignore | 1 - .../petstore/c/.openapi-generator/FILES | 1 + samples/client/petstore/c/CMakeLists.txt | 138 +++++++++++++++--- 3 files changed, 121 insertions(+), 19 deletions(-) diff --git a/samples/client/petstore/c/.openapi-generator-ignore b/samples/client/petstore/c/.openapi-generator-ignore index f574e0daf8fe..7484ee590a38 100644 --- a/samples/client/petstore/c/.openapi-generator-ignore +++ b/samples/client/petstore/c/.openapi-generator-ignore @@ -21,4 +21,3 @@ #docs/*.md # Then explicitly reverse the ignore rule for a single file: #!docs/README.md -CMakeLists.txt diff --git a/samples/client/petstore/c/.openapi-generator/FILES b/samples/client/petstore/c/.openapi-generator/FILES index 48d9373c724d..8c91a58b05a0 100644 --- a/samples/client/petstore/c/.openapi-generator/FILES +++ b/samples/client/petstore/c/.openapi-generator/FILES @@ -1,3 +1,4 @@ +CMakeLists.txt Config.cmake.in Packing.cmake README.md diff --git a/samples/client/petstore/c/CMakeLists.txt b/samples/client/petstore/c/CMakeLists.txt index cca11426642a..cd72ab391660 100644 --- a/samples/client/petstore/c/CMakeLists.txt +++ b/samples/client/petstore/c/CMakeLists.txt @@ -1,33 +1,67 @@ -cmake_minimum_required (VERSION 2.6) -project (CGenerator) +cmake_minimum_required (VERSION 2.6...3.10.2) +project (CGenerator C) cmake_policy(SET CMP0063 NEW) set(CMAKE_C_VISIBILITY_PRESET default) -set(CMAKE_CXX_VISIBILITY_PRESET default) set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF) -set(CMAKE_BUILD_TYPE Debug) +set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) +set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion") + +option(BUILD_SHARED_LIBS "Build using shared libraries" ON) + +find_package(OpenSSL) + +if (OPENSSL_FOUND) + message (STATUS "OPENSSL found") + + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL") + if(CMAKE_VERSION VERSION_LESS 3.4) + include_directories(${OPENSSL_INCLUDE_DIR}) + include_directories(${OPENSSL_INCLUDE_DIRS}) + link_directories(${OPENSSL_LIBRARIES}) + endif() + + message(STATUS "Using OpenSSL ${OPENSSL_VERSION}") +else() + message (STATUS "OpenSSL Not found.") +endif() set(pkgName "openapi_petstore") -find_package(CURL 7.58.0 REQUIRED) -if(CURL_FOUND) - include_directories(${CURL_INCLUDE_DIR}) - set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} ) -else(CURL_FOUND) - message(FATAL_ERROR "Could not find the CURL library and development files.") +# this default version can be overridden in PreTarget.cmake +set(PROJECT_VERSION_MAJOR 0) +set(PROJECT_VERSION_MINOR 0) +set(PROJECT_VERSION_PATCH 1) + +if( (DEFINED CURL_INCLUDE_DIR) AND (DEFINED CURL_LIBRARIES)) + include_directories(${CURL_INCLUDE_DIR}) + set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} ) +else() + find_package(CURL 7.58.0 REQUIRED) + if(CURL_FOUND) + include_directories(${CURL_INCLUDE_DIR}) + set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} ) + else(CURL_FOUND) + message(FATAL_ERROR "Could not find the CURL library and development files.") + endif() endif() set(SRCS src/list.c src/apiKey.c src/apiClient.c + src/binary.c external/cJSON.c model/object.c + model/mapped_model.c model/api_response.c + model/bit.c model/category.c + model/model_with_set_propertes.c model/order.c model/pet.c + model/preference.c model/tag.c model/user.c api/PetAPI.c @@ -39,13 +73,19 @@ set(SRCS set(HDRS include/apiClient.h include/list.h + include/binary.h include/keyValuePair.h external/cJSON.h model/object.h + model/any_type.h + model/mapped_model.h model/api_response.h + model/bit.h model/category.h + model/model_with_set_propertes.h model/order.h model/pet.h + model/preference.h model/tag.h model/user.h api/PetAPI.h @@ -54,12 +94,75 @@ set(HDRS ) -# Add library with project file with projectname as library name -add_library(${pkgName} SHARED ${SRCS} ${HDRS}) +include(PreTarget.cmake OPTIONAL) + +set(PROJECT_VERSION_STRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") + +# Add library with project file with project name as library name +add_library(${pkgName} ${SRCS} ${HDRS}) # Link dependent libraries -target_link_libraries(${pkgName} ${CURL_LIBRARIES} ) -#install library to destination -install(TARGETS ${pkgName} DESTINATION ${CMAKE_INSTALL_PREFIX}) +if(NOT CMAKE_VERSION VERSION_LESS 3.4) + target_link_libraries(${pkgName} PRIVATE OpenSSL::SSL OpenSSL::Crypto) +endif() +target_link_libraries(${pkgName} PUBLIC ${CURL_LIBRARIES} ) +target_include_directories( + ${pkgName} PUBLIC $ + $ +) + +include(PostTarget.cmake OPTIONAL) + +# installation of libraries, headers, and config files +if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in) + install(TARGETS ${pkgName} DESTINATION lib) +else() + include(GNUInstallDirs) + install(TARGETS ${pkgName} DESTINATION lib EXPORT ${pkgName}Targets) + + foreach(HDR_FILE ${HDRS}) + get_filename_component(HDR_DIRECTORY ${HDR_FILE} DIRECTORY) + get_filename_component(ABSOLUTE_HDR_DIRECTORY ${HDR_DIRECTORY} ABSOLUTE) + file(RELATIVE_PATH RELATIVE_HDR_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${ABSOLUTE_HDR_DIRECTORY}) + install(FILES ${HDR_FILE} DESTINATION include/${pkgName}/${RELATIVE_HDR_PATH}) + endforeach() + + include(CMakePackageConfigHelpers) + write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}ConfigVersion.cmake" + VERSION "${PROJECT_VERSION_STRING}" + COMPATIBILITY AnyNewerVersion + ) + + export(EXPORT ${pkgName}Targets + FILE "${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}Targets.cmake" + NAMESPACE ${pkgName}:: + ) + + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}Config.cmake" + @ONLY + ) + + set(ConfigPackageLocation lib/cmake/${pkgName}) + install(EXPORT ${pkgName}Targets + FILE + ${pkgName}Targets.cmake + NAMESPACE + ${pkgName}:: + DESTINATION + ${ConfigPackageLocation} + ) + install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}Config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}ConfigVersion.cmake" + DESTINATION + ${ConfigPackageLocation} + ) +endif() + +# make installation packages +include(Packing.cmake OPTIONAL) # Setting file variables to null set(SRCS "") @@ -72,8 +175,7 @@ set(HDRS "") # unit-tests/manual-PetAPI.c # unit-tests/manual-StoreAPI.c # unit-tests/manual-UserAPI.c -# unit-tests/manual-order.c -# unit-tests/manual-user.c) +#) ##set header files #set(HDRS @@ -81,7 +183,7 @@ set(HDRS "") ## loop over all files in SRCS variable #foreach(SOURCE_FILE ${SRCS}) -# # Get only the file name from the file as add_executable doesn't support executable with slash("/") +# # Get only the file name from the file as add_executable does not support executable with slash("/") # get_filename_component(FILE_NAME_ONLY ${SOURCE_FILE} NAME_WE) # # Remove .c from the file name and set it as executable name # string( REPLACE ".c" "" EXECUTABLE_FILE ${FILE_NAME_ONLY}) From 773b86c5fa9c8578307c3916fe8ad8c19b4176d3 Mon Sep 17 00:00:00 2001 From: Juuso Alasuutari Date: Fri, 10 Jan 2025 14:22:02 +0200 Subject: [PATCH 2/3] [C][Client] Allow user-defined CMAKE_C_FLAGS --- .../src/main/resources/C-libcurl/CMakeLists.txt.mustache | 4 +++- samples/client/others/c/bearerAuth/CMakeLists.txt | 4 +++- samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt | 4 +++- samples/client/petstore/c/CMakeLists.txt | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache index fa59c65e07ba..a60b3e23703b 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache @@ -6,7 +6,9 @@ cmake_policy(SET CMP0063 NEW) set(CMAKE_C_VISIBILITY_PRESET default) set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=missing-declarations") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=int-conversion") option(BUILD_SHARED_LIBS "Build using shared libraries" ON) diff --git a/samples/client/others/c/bearerAuth/CMakeLists.txt b/samples/client/others/c/bearerAuth/CMakeLists.txt index e359d41c7789..4e8cc312387f 100644 --- a/samples/client/others/c/bearerAuth/CMakeLists.txt +++ b/samples/client/others/c/bearerAuth/CMakeLists.txt @@ -6,7 +6,9 @@ cmake_policy(SET CMP0063 NEW) set(CMAKE_C_VISIBILITY_PRESET default) set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=missing-declarations") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=int-conversion") option(BUILD_SHARED_LIBS "Build using shared libraries" ON) diff --git a/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt b/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt index cd72ab391660..a11c893b84cc 100644 --- a/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt +++ b/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt @@ -6,7 +6,9 @@ cmake_policy(SET CMP0063 NEW) set(CMAKE_C_VISIBILITY_PRESET default) set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=missing-declarations") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=int-conversion") option(BUILD_SHARED_LIBS "Build using shared libraries" ON) diff --git a/samples/client/petstore/c/CMakeLists.txt b/samples/client/petstore/c/CMakeLists.txt index cd72ab391660..a11c893b84cc 100644 --- a/samples/client/petstore/c/CMakeLists.txt +++ b/samples/client/petstore/c/CMakeLists.txt @@ -6,7 +6,9 @@ cmake_policy(SET CMP0063 NEW) set(CMAKE_C_VISIBILITY_PRESET default) set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=missing-declarations") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=int-conversion") option(BUILD_SHARED_LIBS "Build using shared libraries" ON) From 54d3a70d06eb74f656981e065521c0f0b33bdddc Mon Sep 17 00:00:00 2001 From: Juuso Alasuutari Date: Fri, 10 Jan 2025 14:54:02 +0200 Subject: [PATCH 3/3] [C][Client] Remove useless message() calls from CMakeLists.txt find_package() prints success/failure messages, no need to do it twice. --- .../src/main/resources/C-libcurl/CMakeLists.txt.mustache | 8 -------- samples/client/others/c/bearerAuth/CMakeLists.txt | 8 -------- .../client/petstore/c-useJsonUnformatted/CMakeLists.txt | 8 -------- samples/client/petstore/c/CMakeLists.txt | 8 -------- 4 files changed, 32 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache index a60b3e23703b..97a44e464432 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache @@ -15,18 +15,12 @@ option(BUILD_SHARED_LIBS "Build using shared libraries" ON) find_package(OpenSSL) if (OPENSSL_FOUND) - message (STATUS "OPENSSL found") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL") if(CMAKE_VERSION VERSION_LESS 3.4) include_directories(${OPENSSL_INCLUDE_DIR}) include_directories(${OPENSSL_INCLUDE_DIRS}) link_directories(${OPENSSL_LIBRARIES}) endif() - - message(STATUS "Using OpenSSL ${OPENSSL_VERSION}") -else() - message (STATUS "OpenSSL Not found.") endif() set(pkgName "{{projectName}}") @@ -44,8 +38,6 @@ else() if(CURL_FOUND) include_directories(${CURL_INCLUDE_DIR}) set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} ) - else(CURL_FOUND) - message(FATAL_ERROR "Could not find the CURL library and development files.") endif() endif() diff --git a/samples/client/others/c/bearerAuth/CMakeLists.txt b/samples/client/others/c/bearerAuth/CMakeLists.txt index 4e8cc312387f..30076da6c6eb 100644 --- a/samples/client/others/c/bearerAuth/CMakeLists.txt +++ b/samples/client/others/c/bearerAuth/CMakeLists.txt @@ -15,18 +15,12 @@ option(BUILD_SHARED_LIBS "Build using shared libraries" ON) find_package(OpenSSL) if (OPENSSL_FOUND) - message (STATUS "OPENSSL found") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL") if(CMAKE_VERSION VERSION_LESS 3.4) include_directories(${OPENSSL_INCLUDE_DIR}) include_directories(${OPENSSL_INCLUDE_DIRS}) link_directories(${OPENSSL_LIBRARIES}) endif() - - message(STATUS "Using OpenSSL ${OPENSSL_VERSION}") -else() - message (STATUS "OpenSSL Not found.") endif() set(pkgName "sample_api") @@ -44,8 +38,6 @@ else() if(CURL_FOUND) include_directories(${CURL_INCLUDE_DIR}) set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} ) - else(CURL_FOUND) - message(FATAL_ERROR "Could not find the CURL library and development files.") endif() endif() diff --git a/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt b/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt index a11c893b84cc..5ad5652ad3a3 100644 --- a/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt +++ b/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt @@ -15,18 +15,12 @@ option(BUILD_SHARED_LIBS "Build using shared libraries" ON) find_package(OpenSSL) if (OPENSSL_FOUND) - message (STATUS "OPENSSL found") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL") if(CMAKE_VERSION VERSION_LESS 3.4) include_directories(${OPENSSL_INCLUDE_DIR}) include_directories(${OPENSSL_INCLUDE_DIRS}) link_directories(${OPENSSL_LIBRARIES}) endif() - - message(STATUS "Using OpenSSL ${OPENSSL_VERSION}") -else() - message (STATUS "OpenSSL Not found.") endif() set(pkgName "openapi_petstore") @@ -44,8 +38,6 @@ else() if(CURL_FOUND) include_directories(${CURL_INCLUDE_DIR}) set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} ) - else(CURL_FOUND) - message(FATAL_ERROR "Could not find the CURL library and development files.") endif() endif() diff --git a/samples/client/petstore/c/CMakeLists.txt b/samples/client/petstore/c/CMakeLists.txt index a11c893b84cc..5ad5652ad3a3 100644 --- a/samples/client/petstore/c/CMakeLists.txt +++ b/samples/client/petstore/c/CMakeLists.txt @@ -15,18 +15,12 @@ option(BUILD_SHARED_LIBS "Build using shared libraries" ON) find_package(OpenSSL) if (OPENSSL_FOUND) - message (STATUS "OPENSSL found") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL") if(CMAKE_VERSION VERSION_LESS 3.4) include_directories(${OPENSSL_INCLUDE_DIR}) include_directories(${OPENSSL_INCLUDE_DIRS}) link_directories(${OPENSSL_LIBRARIES}) endif() - - message(STATUS "Using OpenSSL ${OPENSSL_VERSION}") -else() - message (STATUS "OpenSSL Not found.") endif() set(pkgName "openapi_petstore") @@ -44,8 +38,6 @@ else() if(CURL_FOUND) include_directories(${CURL_INCLUDE_DIR}) set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} ) - else(CURL_FOUND) - message(FATAL_ERROR "Could not find the CURL library and development files.") endif() endif()