Skip to content

Commit 34b91bd

Browse files
committed
Add math library check and build static executable
1 parent 39eddf7 commit 34b91bd

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

CMakeLists.txt

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
# Copyright (c) 2021 Jorge Rivera. All right reserved.
66
# License GNU Lesser General Public License v3.0.
77

8-
cmake_minimum_required(VERSION 3.1)
8+
cmake_minimum_required(VERSION 3.14) # Update to prevent policy CMP0080 warning
99

1010
project(picoder)
1111

1212
# Set C/C++ Standard
13-
set(CMAKE_C_STANDARD 11)
13+
set(CMAKE_C_STANDARD 11) # Needed by glibc getopt in MSVC
1414
set(CMAKE_CXX_STANDARD 11)
1515

1616
# Check for git repository information
@@ -67,7 +67,7 @@ endif()
6767

6868
# Set C/C++ flags
6969
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
70-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter")
70+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter -fcommon") # Add -fcommon CFLAG to prevent multiple-definition
7171
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unused-parameter -Wconversion -Woverloaded-virtual -Wsign-conversion")
7272
elseif(MSVC)
7373
set(MSVC_DISABLED_WARNINGS_LIST
@@ -113,11 +113,36 @@ else()
113113
add_executable( ${PROJECT_NAME} ${SRC} )
114114
endif()
115115

116+
# Add executable export symbols for loadable modules to prevent policy CMP0065 warning
117+
set_target_properties( ${PROJECT_NAME} PROPERTIES ENABLE_EXPORTS TRUE )
118+
119+
set_target_properties( ${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE TRUE ) # Add -fPIC
120+
121+
set_target_properties( ${PROJECT_NAME} PROPERTIES LINK_SEARCH_START_STATIC TRUE ) # Add "-Bstatic"
122+
123+
# Build static executable if possible
124+
if( NOT (CMAKE_SYSTEM_NAME MATCHES "Darwin" OR MSVC) )
125+
set( CMAKE_EXE_LINKER_FLAGS "-static" )
126+
endif()
127+
128+
# Checking for math library 'libm' used when including <math.h> in pilight sources
129+
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.lib;.tbd;.so;.dylib;.dll.a")
130+
find_library(MATH_LIBRARY NAMES m )
131+
if(MATH_LIBRARY)
132+
MESSAGE( STATUS "Math library for ${PROJECT_NAME}: " ${MATH_LIBRARY} )
133+
else()
134+
if(NOT MSVC)
135+
message(FATAL_ERROR "Cannot find math library 'libm' ")
136+
else()
137+
set(MATH_LIBRARY "")
138+
endif()
139+
endif()
140+
116141
# Add include directory to use #include <PiCode.h>
117142
target_include_directories( ${PROJECT_NAME} PRIVATE libs/PiCode/src/ )
118143

119-
# Add PiCode static library (libpicode.a) to link picoder executable
120-
target_link_libraries( ${PROJECT_NAME} PUBLIC picode )
144+
# Add PiCode and Math static libraries to link picoder executable
145+
target_link_libraries( ${PROJECT_NAME} PRIVATE picode ${MATH_LIBRARY})
121146

122147
# If git info available adds to picoder executable as environment var
123148
if(DEFINED BUILD_VERSION)

0 commit comments

Comments
 (0)