Skip to content

Commit 871876f

Browse files
czurniedensjaeckel
authored andcommitted
Made "test" work with shared lib, started gathering environment variables for CFLAGS
1 parent c544108 commit 871876f

File tree

1 file changed

+114
-5
lines changed

1 file changed

+114
-5
lines changed

CMakeLists.txt

Lines changed: 114 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,124 @@ include(GNUInstallDirs)
1313
include(CMakePackageConfigHelpers)
1414
include(sources.cmake)
1515

16+
# The only direct cmake argument for now
17+
option(BUILD_SHARED_LIBS "Build shared library and only the shared library if \"ON\", default is static" OFF)
18+
1619
#-----------------------------------------------------------------------------
17-
# Options
20+
# Compose CFLAGS
1821
#-----------------------------------------------------------------------------
19-
option(BUILD_SHARED_LIBS "Build shared library" FALSE)
22+
23+
# check if there was one already set.
24+
if(DEFINED ENV{LTM_CFLAGS})
25+
set(LTM_C_FLAGS $ENV{LTM_CFLAGS})
26+
endif()
27+
if(DEFINED ENV{LTM_LDFLAGS})
28+
set(LTM_LD_FLAGS $ENV{LTM_LDFLAGS})
29+
endif()
30+
31+
# Some information copied from makefile_include.mk
32+
33+
# Basic set
34+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wall -Wsign-compare -Wextra -Wshadow")
35+
36+
# Do we run the sanitizer?
37+
if(DEFINED ENV{SANITIZER})
38+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero")
39+
endif()
40+
41+
if(NOT DEFINED ENV{NO_ADDTL_WARNINGS})
42+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align")
43+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wstrict-prototypes -Wpointer-arith")
44+
endif()
45+
46+
if(DEFINED ENV{CONV_WARNINGS})
47+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -std=c89 -Wconversion -Wsign-conversion")
48+
if($ENV{CONV_WARNINGS} EQUAL "strict")
49+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wc++-compat")
50+
endif()
51+
else()
52+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wsystem-headers")
53+
endif()
54+
55+
if(DEFINED ENV{COMPILE_DEBUG})
56+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -g3")
57+
endif()
58+
59+
if(DEFINED ENV{COMPILE_SIZE})
60+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Os")
61+
else()
62+
if(NOT DEFINED ENV{IGNORE_SPEED})
63+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -O3 -funroll-loops")
64+
#x86 optimizations [should be valid for any GCC install though]
65+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -fomit-frame-pointer")
66+
endif()
67+
# TODO:
68+
# if(DEFINED ENV{COMPILE_LTO})
69+
# set(LTM_C_FLAGS "${LTM_C_FLAGS} -flto")
70+
# set(LTM_LD_FLAGS "${LTM_LD_FLAGS} -flto")
71+
# #AR = $(subst clang,llvm-ar,$(subst gcc,gcc-ar,$(CC)))
72+
# endif()
73+
endif()
74+
75+
# What compiler do we have and what are their...uhm... peculiarities
76+
# TODO: is the check for a C++ compiler necessary?
77+
if( (CMAKE_C_COMPILER_ID MATCHES "(C|c?)lang") OR (CMAKE_CXX_COMPILER_ID MATCHES "(C|c?)lang"))
78+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header")
79+
endif()
80+
81+
if( (CMAKE_C_COMPILER_ID MATCHES "mingw") OR (CMAKE_CXX_COMPILER_ID MATCHES "mingw"))
82+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wno-shadow")
83+
endif()
84+
85+
if(DEFINED ENV{PLATFORM})
86+
if($ENV{PLATFORM} MATCHES "Darwin")
87+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wno-nullability-completeness")
88+
endif()
89+
if($ENV{PLATFORM} MATCHES "CYGWIN")
90+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -no-undefined")
91+
endif()
92+
endif()
93+
94+
# TODO: coverage (lgcov)
95+
96+
# We have several private functions in the library and the "demo/test" programm
97+
# needs a littkle note to be able to switch them off. Please use the static build
98+
# to get a full test.
99+
if(BUILD_SHARED_LIBS)
100+
set(LTM_C_FLAGS "${LTM_C_FLAGS} -DLTM_TEST_DYNAMIC")
101+
endif()
102+
103+
# Bring it home
104+
set(CMAKE_C_FLAGS "${LTM_C_FLAGS}")
105+
set(CMAKE_C_FLAGS_DEBUG "${LTM_C_FLAGS}")
106+
set(CMAKE_C_FLAGS_RELEASE "${LTM_C_FLAGS}")
20107

21108
#-----------------------------------------------------------------------------
22109
# library target
23110
#-----------------------------------------------------------------------------
24-
add_library(${PROJECT_NAME}
25-
${SOURCES}
111+
112+
# TODO: There may be a way but I found none to build both at once without complication.
113+
# It is possible with e.g. Linux where the static library is named libtommath.a
114+
# and the dynamic library libtommath.so*, two different names.
115+
# That is not the case with e.g. Windows where both types have the same name.
116+
# See also:
117+
# https://stackoverflow.com/questions/2152077/is-it-possible-to-get-cmake-to-build-both-a-static-and-shared-library-at-the-sam
118+
if(BUILD_SHARED_LIBS)
119+
add_library(${PROJECT_NAME} SHARED
120+
${SOURCES}
121+
)
122+
else()
123+
add_library(${PROJECT_NAME} STATIC
124+
${SOURCES}
125+
)
126+
endif()
127+
128+
# Clear cache manually
129+
unset(BUILD_SHARED_LIBS CACHE)
130+
131+
target_include_directories(${PROJECT_NAME} PUBLIC
132+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
133+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>
26134
)
27135

28136
target_include_directories(${PROJECT_NAME} PUBLIC
@@ -36,6 +144,8 @@ set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION ${PROJECT_VERSION_MAJ
36144
#-----------------------------------------------------------------------------
37145
# demo target
38146
#-----------------------------------------------------------------------------
147+
148+
39149
add_executable(test-target EXCLUDE_FROM_ALL
40150
${CMAKE_CURRENT_SOURCE_DIR}/demo/shared.c
41151
${CMAKE_CURRENT_SOURCE_DIR}/demo/test.c
@@ -81,7 +191,6 @@ set(TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
81191

82192
install(TARGETS ${PROJECT_NAME}
83193
EXPORT ${TARGETS_EXPORT_NAME}
84-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
85194
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
86195
)
87196

0 commit comments

Comments
 (0)