Skip to content

Commit c7b3e9f

Browse files
imaamitimon-sbr
authored andcommitted
[C][Client] Make custom CMAKE_C_FLAGS work (OpenAPITools#20432)
* [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. * [C][Client] Allow user-defined CMAKE_C_FLAGS * [C][Client] Remove useless message() calls from CMakeLists.txt find_package() prints success/failure messages, no need to do it twice.
1 parent 2390140 commit c7b3e9f

File tree

6 files changed

+124
-46
lines changed

6 files changed

+124
-46
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache

+3-9
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,21 @@ cmake_policy(SET CMP0063 NEW)
66
set(CMAKE_C_VISIBILITY_PRESET default)
77
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
88
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
9-
set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion")
9+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration")
10+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=missing-declarations")
11+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=int-conversion")
1012

1113
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
1214

1315
find_package(OpenSSL)
1416

1517
if (OPENSSL_FOUND)
16-
message (STATUS "OPENSSL found")
17-
1818
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL")
1919
if(CMAKE_VERSION VERSION_LESS 3.4)
2020
include_directories(${OPENSSL_INCLUDE_DIR})
2121
include_directories(${OPENSSL_INCLUDE_DIRS})
2222
link_directories(${OPENSSL_LIBRARIES})
2323
endif()
24-
25-
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
26-
else()
27-
message (STATUS "OpenSSL Not found.")
2824
endif()
2925

3026
set(pkgName "{{projectName}}")
@@ -42,8 +38,6 @@ else()
4238
if(CURL_FOUND)
4339
include_directories(${CURL_INCLUDE_DIR})
4440
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
45-
else(CURL_FOUND)
46-
message(FATAL_ERROR "Could not find the CURL library and development files.")
4741
endif()
4842
endif()
4943

samples/client/others/c/bearerAuth/CMakeLists.txt

+3-9
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,21 @@ cmake_policy(SET CMP0063 NEW)
66
set(CMAKE_C_VISIBILITY_PRESET default)
77
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
88
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
9-
set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion")
9+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration")
10+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=missing-declarations")
11+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=int-conversion")
1012

1113
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
1214

1315
find_package(OpenSSL)
1416

1517
if (OPENSSL_FOUND)
16-
message (STATUS "OPENSSL found")
17-
1818
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL")
1919
if(CMAKE_VERSION VERSION_LESS 3.4)
2020
include_directories(${OPENSSL_INCLUDE_DIR})
2121
include_directories(${OPENSSL_INCLUDE_DIRS})
2222
link_directories(${OPENSSL_LIBRARIES})
2323
endif()
24-
25-
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
26-
else()
27-
message (STATUS "OpenSSL Not found.")
2824
endif()
2925

3026
set(pkgName "sample_api")
@@ -42,8 +38,6 @@ else()
4238
if(CURL_FOUND)
4339
include_directories(${CURL_INCLUDE_DIR})
4440
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
45-
else(CURL_FOUND)
46-
message(FATAL_ERROR "Could not find the CURL library and development files.")
4741
endif()
4842
endif()
4943

samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt

+3-9
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,21 @@ cmake_policy(SET CMP0063 NEW)
66
set(CMAKE_C_VISIBILITY_PRESET default)
77
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
88
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
9-
set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion")
9+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration")
10+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=missing-declarations")
11+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=int-conversion")
1012

1113
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
1214

1315
find_package(OpenSSL)
1416

1517
if (OPENSSL_FOUND)
16-
message (STATUS "OPENSSL found")
17-
1818
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL")
1919
if(CMAKE_VERSION VERSION_LESS 3.4)
2020
include_directories(${OPENSSL_INCLUDE_DIR})
2121
include_directories(${OPENSSL_INCLUDE_DIRS})
2222
link_directories(${OPENSSL_LIBRARIES})
2323
endif()
24-
25-
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
26-
else()
27-
message (STATUS "OpenSSL Not found.")
2824
endif()
2925

3026
set(pkgName "openapi_petstore")
@@ -42,8 +38,6 @@ else()
4238
if(CURL_FOUND)
4339
include_directories(${CURL_INCLUDE_DIR})
4440
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
45-
else(CURL_FOUND)
46-
message(FATAL_ERROR "Could not find the CURL library and development files.")
4741
endif()
4842
endif()
4943

samples/client/petstore/c/.openapi-generator-ignore

-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@
2121
#docs/*.md
2222
# Then explicitly reverse the ignore rule for a single file:
2323
#!docs/README.md
24-
CMakeLists.txt

samples/client/petstore/c/.openapi-generator/FILES

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
CMakeLists.txt
12
Config.cmake.in
23
Packing.cmake
34
README.md

samples/client/petstore/c/CMakeLists.txt

+114-18
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,61 @@
1-
cmake_minimum_required (VERSION 2.6)
2-
project (CGenerator)
1+
cmake_minimum_required (VERSION 2.6...3.10.2)
2+
project (CGenerator C)
33

44
cmake_policy(SET CMP0063 NEW)
55

66
set(CMAKE_C_VISIBILITY_PRESET default)
7-
set(CMAKE_CXX_VISIBILITY_PRESET default)
87
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
9-
set(CMAKE_BUILD_TYPE Debug)
8+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
9+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration")
10+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=missing-declarations")
11+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=int-conversion")
12+
13+
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
14+
15+
find_package(OpenSSL)
16+
17+
if (OPENSSL_FOUND)
18+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL")
19+
if(CMAKE_VERSION VERSION_LESS 3.4)
20+
include_directories(${OPENSSL_INCLUDE_DIR})
21+
include_directories(${OPENSSL_INCLUDE_DIRS})
22+
link_directories(${OPENSSL_LIBRARIES})
23+
endif()
24+
endif()
1025

1126
set(pkgName "openapi_petstore")
1227

13-
find_package(CURL 7.58.0 REQUIRED)
14-
if(CURL_FOUND)
15-
include_directories(${CURL_INCLUDE_DIR})
16-
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
17-
else(CURL_FOUND)
18-
message(FATAL_ERROR "Could not find the CURL library and development files.")
28+
# this default version can be overridden in PreTarget.cmake
29+
set(PROJECT_VERSION_MAJOR 0)
30+
set(PROJECT_VERSION_MINOR 0)
31+
set(PROJECT_VERSION_PATCH 1)
32+
33+
if( (DEFINED CURL_INCLUDE_DIR) AND (DEFINED CURL_LIBRARIES))
34+
include_directories(${CURL_INCLUDE_DIR})
35+
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
36+
else()
37+
find_package(CURL 7.58.0 REQUIRED)
38+
if(CURL_FOUND)
39+
include_directories(${CURL_INCLUDE_DIR})
40+
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
41+
endif()
1942
endif()
2043

2144
set(SRCS
2245
src/list.c
2346
src/apiKey.c
2447
src/apiClient.c
48+
src/binary.c
2549
external/cJSON.c
2650
model/object.c
51+
model/mapped_model.c
2752
model/api_response.c
53+
model/bit.c
2854
model/category.c
55+
model/model_with_set_propertes.c
2956
model/order.c
3057
model/pet.c
58+
model/preference.c
3159
model/tag.c
3260
model/user.c
3361
api/PetAPI.c
@@ -39,13 +67,19 @@ set(SRCS
3967
set(HDRS
4068
include/apiClient.h
4169
include/list.h
70+
include/binary.h
4271
include/keyValuePair.h
4372
external/cJSON.h
4473
model/object.h
74+
model/any_type.h
75+
model/mapped_model.h
4576
model/api_response.h
77+
model/bit.h
4678
model/category.h
79+
model/model_with_set_propertes.h
4780
model/order.h
4881
model/pet.h
82+
model/preference.h
4983
model/tag.h
5084
model/user.h
5185
api/PetAPI.h
@@ -54,12 +88,75 @@ set(HDRS
5488

5589
)
5690

57-
# Add library with project file with projectname as library name
58-
add_library(${pkgName} SHARED ${SRCS} ${HDRS})
91+
include(PreTarget.cmake OPTIONAL)
92+
93+
set(PROJECT_VERSION_STRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
94+
95+
# Add library with project file with project name as library name
96+
add_library(${pkgName} ${SRCS} ${HDRS})
5997
# Link dependent libraries
60-
target_link_libraries(${pkgName} ${CURL_LIBRARIES} )
61-
#install library to destination
62-
install(TARGETS ${pkgName} DESTINATION ${CMAKE_INSTALL_PREFIX})
98+
if(NOT CMAKE_VERSION VERSION_LESS 3.4)
99+
target_link_libraries(${pkgName} PRIVATE OpenSSL::SSL OpenSSL::Crypto)
100+
endif()
101+
target_link_libraries(${pkgName} PUBLIC ${CURL_LIBRARIES} )
102+
target_include_directories(
103+
${pkgName} PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
104+
$<INSTALL_INTERFACE:include>
105+
)
106+
107+
include(PostTarget.cmake OPTIONAL)
108+
109+
# installation of libraries, headers, and config files
110+
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in)
111+
install(TARGETS ${pkgName} DESTINATION lib)
112+
else()
113+
include(GNUInstallDirs)
114+
install(TARGETS ${pkgName} DESTINATION lib EXPORT ${pkgName}Targets)
115+
116+
foreach(HDR_FILE ${HDRS})
117+
get_filename_component(HDR_DIRECTORY ${HDR_FILE} DIRECTORY)
118+
get_filename_component(ABSOLUTE_HDR_DIRECTORY ${HDR_DIRECTORY} ABSOLUTE)
119+
file(RELATIVE_PATH RELATIVE_HDR_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${ABSOLUTE_HDR_DIRECTORY})
120+
install(FILES ${HDR_FILE} DESTINATION include/${pkgName}/${RELATIVE_HDR_PATH})
121+
endforeach()
122+
123+
include(CMakePackageConfigHelpers)
124+
write_basic_package_version_file(
125+
"${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}ConfigVersion.cmake"
126+
VERSION "${PROJECT_VERSION_STRING}"
127+
COMPATIBILITY AnyNewerVersion
128+
)
129+
130+
export(EXPORT ${pkgName}Targets
131+
FILE "${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}Targets.cmake"
132+
NAMESPACE ${pkgName}::
133+
)
134+
135+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
136+
"${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}Config.cmake"
137+
@ONLY
138+
)
139+
140+
set(ConfigPackageLocation lib/cmake/${pkgName})
141+
install(EXPORT ${pkgName}Targets
142+
FILE
143+
${pkgName}Targets.cmake
144+
NAMESPACE
145+
${pkgName}::
146+
DESTINATION
147+
${ConfigPackageLocation}
148+
)
149+
install(
150+
FILES
151+
"${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}Config.cmake"
152+
"${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}ConfigVersion.cmake"
153+
DESTINATION
154+
${ConfigPackageLocation}
155+
)
156+
endif()
157+
158+
# make installation packages
159+
include(Packing.cmake OPTIONAL)
63160

64161
# Setting file variables to null
65162
set(SRCS "")
@@ -72,16 +169,15 @@ set(HDRS "")
72169
# unit-tests/manual-PetAPI.c
73170
# unit-tests/manual-StoreAPI.c
74171
# unit-tests/manual-UserAPI.c
75-
# unit-tests/manual-order.c
76-
# unit-tests/manual-user.c)
172+
#)
77173

78174
##set header files
79175
#set(HDRS
80176
#)
81177

82178
## loop over all files in SRCS variable
83179
#foreach(SOURCE_FILE ${SRCS})
84-
# # Get only the file name from the file as add_executable doesn't support executable with slash("/")
180+
# # Get only the file name from the file as add_executable does not support executable with slash("/")
85181
# get_filename_component(FILE_NAME_ONLY ${SOURCE_FILE} NAME_WE)
86182
# # Remove .c from the file name and set it as executable name
87183
# string( REPLACE ".c" "" EXECUTABLE_FILE ${FILE_NAME_ONLY})

0 commit comments

Comments
 (0)