Skip to content

Commit df31777

Browse files
committed
Support build proto as shared lib on Windows
1 parent 930b694 commit df31777

File tree

2 files changed

+153
-29
lines changed

2 files changed

+153
-29
lines changed

cmake/opentelemetry-proto.cmake

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
#
5-
# The dependency on opentelemetry-proto can be provided by order
6-
# of decreasing priority, options are:
5+
# The dependency on opentelemetry-proto can be provided by order of decreasing
6+
# priority, options are:
77
#
8-
# 1 - Fetch from local source directory defined by the OTELCPP_PROTO_PATH variable
8+
# 1 - Fetch from local source directory defined by the OTELCPP_PROTO_PATH
9+
# variable
910
#
10-
# 2 - Fetch from the opentelemetry-proto git submodule (opentelemetry-cpp/third_party/opentelemetry-proto)
11+
# 2 - Fetch from the opentelemetry-proto git submodule
12+
# (opentelemetry-cpp/third_party/opentelemetry-proto)
1113
#
12-
# 3 - Fetch from github using the git tag set in opentelemetry-cpp/third_party_release
14+
# 3 - Fetch from github using the git tag set in
15+
# opentelemetry-cpp/third_party_release
1316
#
1417

15-
set(OPENTELEMETRY_PROTO_SUBMODULE "${opentelemetry-cpp_SOURCE_DIR}/third_party/opentelemetry-proto")
18+
set(OPENTELEMETRY_PROTO_SUBMODULE
19+
"${opentelemetry-cpp_SOURCE_DIR}/third_party/opentelemetry-proto")
1620

1721
if(OTELCPP_PROTO_PATH)
1822
if(NOT EXISTS
@@ -21,29 +25,32 @@ if(OTELCPP_PROTO_PATH)
2125
FATAL_ERROR
2226
"OTELCPP_PROTO_PATH does not point to a opentelemetry-proto repository")
2327
endif()
24-
message(STATUS "fetching opentelemetry-proto from OTELCPP_PROTO_PATH=${OTELCPP_PROTO_PATH}")
25-
FetchContent_Declare(
26-
opentelemetry-proto
27-
SOURCE_DIR ${OTELCPP_PROTO_PATH}
28+
message(
29+
STATUS
30+
"fetching opentelemetry-proto from OTELCPP_PROTO_PATH=${OTELCPP_PROTO_PATH}"
2831
)
32+
FetchContent_Declare(opentelemetry-proto SOURCE_DIR ${OTELCPP_PROTO_PATH})
2933
set(opentelemetry-proto_PROVIDER "fetch_source")
30-
# If the opentelemetry-proto directory is a general directory then we don't have a good way to determine the version. Set it as unknown.
34+
# If the opentelemetry-proto directory is a general directory then we don't
35+
# have a good way to determine the version. Set it as unknown.
3136
set(opentelemetry-proto_VERSION "unknown")
3237
elseif(EXISTS ${OPENTELEMETRY_PROTO_SUBMODULE}/.git)
33-
message(STATUS "fetching opentelemetry-proto from git submodule")
34-
FetchContent_Declare(
35-
opentelemetry-proto
36-
SOURCE_DIR ${OPENTELEMETRY_PROTO_SUBMODULE}
37-
)
38+
message(STATUS "fetching opentelemetry-proto from git submodule")
39+
FetchContent_Declare(opentelemetry-proto SOURCE_DIR
40+
${OPENTELEMETRY_PROTO_SUBMODULE})
3841
set(opentelemetry-proto_PROVIDER "fetch_source")
39-
string(REGEX REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1" opentelemetry-proto_VERSION "${opentelemetry-proto_GIT_TAG}")
42+
string(REGEX
43+
REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1"
44+
opentelemetry-proto_VERSION "${opentelemetry-proto_GIT_TAG}")
4045
else()
4146
FetchContent_Declare(
42-
opentelemetry-proto
43-
GIT_REPOSITORY https://github.com/open-telemetry/opentelemetry-proto.git
44-
GIT_TAG "${opentelemetry-proto_GIT_TAG}")
47+
opentelemetry-proto
48+
GIT_REPOSITORY https://github.com/open-telemetry/opentelemetry-proto.git
49+
GIT_TAG "${opentelemetry-proto_GIT_TAG}")
4550
set(opentelemetry-proto_PROVIDER "fetch_repository")
46-
string(REGEX REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1" opentelemetry-proto_VERSION "${opentelemetry-proto_GIT_TAG}")
51+
string(REGEX
52+
REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1"
53+
opentelemetry-proto_VERSION "${opentelemetry-proto_GIT_TAG}")
4754
endif()
4855

4956
FetchContent_MakeAvailable(opentelemetry-proto)
@@ -174,8 +181,10 @@ foreach(IMPORT_DIR ${PROTOBUF_IMPORT_DIRS})
174181
list(APPEND PROTOBUF_INCLUDE_FLAGS "-I${IMPORT_DIR}")
175182
endforeach()
176183

177-
set(PROTOBUF_COMMON_FLAGS "--proto_path=${PROTO_PATH}"
178-
"--cpp_out=${GENERATED_PROTOBUF_PATH}")
184+
set(PROTOBUF_COMMON_FLAGS
185+
"--proto_path=${PROTO_PATH}"
186+
"--cpp_out=dllexport_decl=OPENTELEMETRY_PROTO_API:${GENERATED_PROTOBUF_PATH}"
187+
)
179188
# --experimental_allow_proto3_optional is available from 3.13 and be stable and
180189
# enabled by default from 3.16
181190
if(Protobuf_VERSION AND Protobuf_VERSION VERSION_LESS "3.16")
@@ -276,13 +285,13 @@ add_custom_command(
276285
DEPENDS ${PROTOBUF_GENERATE_DEPENDS})
277286

278287
unset(OTELCPP_PROTO_TARGET_OPTIONS)
279-
if(CMAKE_SYSTEM_NAME MATCHES "Windows|MinGW|WindowsStore")
280-
list(APPEND OTELCPP_PROTO_TARGET_OPTIONS STATIC)
281-
elseif((NOT protobuf_lib_type STREQUAL "STATIC_LIBRARY")
282-
AND (NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS))
288+
if((NOT protobuf_lib_type STREQUAL "STATIC_LIBRARY")
289+
AND (NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS))
283290
list(APPEND OTELCPP_PROTO_TARGET_OPTIONS SHARED)
291+
set(OTELCPP_PROTO_LIB_TYPE "SHARED_LIBRARY")
284292
else()
285293
list(APPEND OTELCPP_PROTO_TARGET_OPTIONS STATIC)
294+
set(OTELCPP_PROTO_LIB_TYPE "STATIC_LIBRARY")
286295
endif()
287296

288297
set(OPENTELEMETRY_PROTO_TARGETS opentelemetry_proto)
@@ -299,6 +308,14 @@ add_library(
299308
${METRICS_SERVICE_PB_CPP_FILE})
300309
set_target_version(opentelemetry_proto)
301310

311+
if(OTELCPP_PROTO_LIB_TYPE STREQUAL "SHARED_LIBRARY")
312+
project_build_tools_set_shared_library_declaration(OPENTELEMETRY_PROTO_API
313+
opentelemetry_proto)
314+
else()
315+
project_build_tools_set_static_library_declaration(OPENTELEMETRY_PROTO_API
316+
opentelemetry_proto)
317+
endif()
318+
302319
target_include_directories(
303320
opentelemetry_proto PUBLIC "$<BUILD_INTERFACE:${GENERATED_PROTOBUF_PATH}>"
304321
"$<INSTALL_INTERFACE:include>")
@@ -323,10 +340,20 @@ if(WITH_OTLP_GRPC)
323340
${LOGS_SERVICE_GRPC_PB_CPP_FILE} ${METRICS_SERVICE_GRPC_PB_CPP_FILE})
324341
set_target_version(opentelemetry_proto_grpc)
325342

343+
if(OTELCPP_PROTO_LIB_TYPE STREQUAL "SHARED_LIBRARY")
344+
if(CMAKE_SYSTEM_NAME MATCHES "Windows|MinGW|WindowsStore")
345+
set_target_properties(opentelemetry_proto_grpc
346+
PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
347+
endif()
348+
endif()
349+
326350
# Disable include-what-you-use and clang-tidy on generated code.
327351
set_target_properties(
328-
opentelemetry_proto_grpc PROPERTIES CXX_INCLUDE_WHAT_YOU_USE ""
329-
CXX_CLANG_TIDY "")
352+
opentelemetry_proto_grpc
353+
PROPERTIES CXX_INCLUDE_WHAT_YOU_USE ""
354+
CXX_CLANG_TIDY ""
355+
C_VISIBILITY_PRESET "default"
356+
CXX_VISIBILITY_PRESET "default")
330357

331358
list(APPEND OPENTELEMETRY_PROTO_TARGETS opentelemetry_proto_grpc)
332359
target_link_libraries(opentelemetry_proto_grpc PUBLIC opentelemetry_proto)

cmake/tools.cmake

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,100 @@ function(project_build_tools_patch_default_imported_config)
222222
endif()
223223
endforeach()
224224
endfunction()
225+
226+
function(project_build_tools_set_export_declaration OUTPUT_VARNAME)
227+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang|Intel|XL|XLClang")
228+
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
229+
set(${OUTPUT_VARNAME}
230+
"__attribute__((__dllexport__))"
231+
PARENT_SCOPE)
232+
else()
233+
set(${OUTPUT_VARNAME}
234+
"__attribute__((visibility(\"default\")))"
235+
PARENT_SCOPE)
236+
endif()
237+
elseif(MSVC)
238+
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
239+
set(${OUTPUT_VARNAME}
240+
"__declspec(dllexport)"
241+
PARENT_SCOPE)
242+
else()
243+
set(${OUTPUT_VARNAME}
244+
""
245+
PARENT_SCOPE)
246+
endif()
247+
elseif(SunPro)
248+
set(${OUTPUT_VARNAME}
249+
"__global"
250+
PARENT_SCOPE)
251+
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
252+
set(${OUTPUT_VARNAME}
253+
"__declspec(dllexport)"
254+
PARENT_SCOPE)
255+
else()
256+
set(${OUTPUT_VARNAME}
257+
""
258+
PARENT_SCOPE)
259+
endif()
260+
endfunction()
261+
262+
function(project_build_tools_set_import_declaration OUTPUT_VARNAME)
263+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang|Intel|XL|XLClang")
264+
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
265+
set(${OUTPUT_VARNAME}
266+
"__attribute__((__dllimport__))"
267+
PARENT_SCOPE)
268+
else()
269+
set(${OUTPUT_VARNAME}
270+
""
271+
PARENT_SCOPE)
272+
endif()
273+
elseif(MSVC)
274+
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
275+
set(${OUTPUT_VARNAME}
276+
"__declspec(dllimport)"
277+
PARENT_SCOPE)
278+
else()
279+
set(${OUTPUT_VARNAME}
280+
""
281+
PARENT_SCOPE)
282+
endif()
283+
elseif(SunPro)
284+
set(${OUTPUT_VARNAME}
285+
"__global"
286+
PARENT_SCOPE)
287+
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
288+
set(${OUTPUT_VARNAME}
289+
"__declspec(dllimport)"
290+
PARENT_SCOPE)
291+
else()
292+
set(${OUTPUT_VARNAME}
293+
""
294+
PARENT_SCOPE)
295+
endif()
296+
endfunction()
297+
298+
function(project_build_tools_set_shared_library_declaration DEFINITION_VARNAME)
299+
project_build_tools_set_export_declaration(EXPORT_DECLARATION)
300+
project_build_tools_set_import_declaration(IMPORT_DECLARATION)
301+
foreach(TARGET_NAME ${ARGN})
302+
target_compile_definitions(
303+
${TARGET_NAME} INTERFACE "${DEFINITION_VARNAME}=${IMPORT_DECLARATION}")
304+
target_compile_definitions(
305+
${TARGET_NAME} PRIVATE "${DEFINITION_VARNAME}=${EXPORT_DECLARATION}")
306+
endforeach()
307+
endfunction()
308+
309+
function(project_build_tools_set_static_library_declaration DEFINITION_VARNAME)
310+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang|Intel|XL|XLClang")
311+
foreach(TARGET_NAME ${ARGN})
312+
target_compile_definitions(
313+
${TARGET_NAME}
314+
PUBLIC "${DEFINITION_VARNAME}=__attribute__((visibility(\"default\")))")
315+
endforeach()
316+
else()
317+
foreach(TARGET_NAME ${ARGN})
318+
target_compile_definitions(${TARGET_NAME} PUBLIC "${DEFINITION_VARNAME}=")
319+
endforeach()
320+
endif()
321+
endfunction()

0 commit comments

Comments
 (0)