@@ -8,7 +8,7 @@ cmake_minimum_required(VERSION 3.1)
88project (picode)
99
1010# Set version number for shared libraries and executables
11- set (CU_VERSION 1.1 ) # current version
11+ set (CU_VERSION 1.2 ) # current version
1212set (SO_VERSION 1.1) # compatibility version
1313
1414# Set C/C++ Standard
@@ -94,29 +94,62 @@ elseif(MSVC)
9494 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -W4 ${MSVC_DISABLED_WARNINGS_STR} " )
9595endif ()
9696
97- # Add source directory for sources
98- AUX_SOURCE_DIRECTORY ( src/ ${PROJECT_NAME} _SRC )
97+ # Add sources for C++ library
98+ FILE (GLOB ${PROJECT_NAME} _SRC_CPP src/*.cpp )
9999
100- # Add library aditional sources
100+ # Pure C PiCode Library sources
101+ FILE (GLOB ${PROJECT_NAME} _SRC_C src/*.c )
102+
103+ # Add library common sources
101104AUX_SOURCE_DIRECTORY ( libs/pilight/libs/pilight/core/ CORE )
102105AUX_SOURCE_DIRECTORY ( libs/pilight/libs/pilight/protocols/ PROTOCOL )
103106AUX_SOURCE_DIRECTORY ( libs/pilight/libs/pilight/protocols/433.92/ PROTOCOLS )
104107
105- # Compile as object library
108+ # Compile common library objects
106109add_library (
107- ${PROJECT_NAME} -obj OBJECT
108- ${${PROJECT_NAME} _SRC}
110+ ${PROJECT_NAME} -common OBJECT
109111 ${CORE}
110112 ${PROTOCOL}
111113 ${PROTOCOLS}
112114)
113115
116+ # Compile C++ library as object
117+ add_library (
118+ ${PROJECT_NAME} -obj OBJECT
119+ ${${PROJECT_NAME} _SRC_CPP}
120+ )
121+
122+ # Pure C PiCode Library compile as object
123+ add_library (
124+ c${PROJECT_NAME} -obj OBJECT
125+ ${${PROJECT_NAME} _SRC_C}
126+ )
127+ # If git info available adds to pure C PiCode Library
128+ if (DEFINED BUILD_VERSION)
129+ target_compile_definitions ( c${PROJECT_NAME} -obj PRIVATE BUILD_VERSION=${BUILD_VERSION} )
130+ endif ()
131+
132+ # If version number for shared libraries and executables available adds to pure C PiCode Library
133+ if (DEFINED CU_VERSION)
134+ target_compile_definitions ( c${PROJECT_NAME} -obj PRIVATE CU_VERSION=${CU_VERSION} )
135+ endif ()
136+
114137# Shared libraries need flag -fPIC
115- set_property (TARGET ${PROJECT_NAME} -obj PROPERTY POSITION_INDEPENDENT_CODE 1)
138+ set_property (TARGET ${PROJECT_NAME} -common PROPERTY POSITION_INDEPENDENT_CODE 1)
139+ set_property (TARGET ${PROJECT_NAME} -obj PROPERTY POSITION_INDEPENDENT_CODE 1)
140+ # Pure C PiCode Library
141+ set_property (TARGET c${PROJECT_NAME} -obj PROPERTY POSITION_INDEPENDENT_CODE 1)
116142
117143# Shared library built from the same object files
144+ add_library (
145+ ${PROJECT_NAME} -dynamic SHARED
146+ $<TARGET_OBJECTS:${PROJECT_NAME} -obj>
147+ $<TARGET_OBJECTS:${PROJECT_NAME} -common>
148+ )
149+ # C++ PiCode Library v1.2 require the pure C library (dynamic or static) as "c${PROJECT_NAME}-dynamic"
150+ target_link_libraries (${PROJECT_NAME} -dynamic PUBLIC c${PROJECT_NAME} )
151+
118152# File extension OS depends, like: libpicode.so or libpicode.dylib or libpicode.dll
119- add_library ( ${PROJECT_NAME} -dynamic SHARED $<TARGET_OBJECTS:${PROJECT_NAME} -obj> )
120153set_target_properties ( ${PROJECT_NAME} -dynamic PROPERTIES OUTPUT_NAME ${PROJECT_NAME} )
121154
122155# Set version numbers for the versioned shared libraries target.
@@ -134,13 +167,59 @@ set_target_properties( ${PROJECT_NAME}-dynamic PROPERTIES
134167 VERSION ${CU_VERSION}
135168)
136169
137- # Add static library libpicode.a
138- add_library ( ${PROJECT_NAME} STATIC $<TARGET_OBJECTS:${PROJECT_NAME} -obj> )
170+ # Add static library
171+ # File extension OS depends, like: libpicode.a or libpicode.lib
172+ add_library (
173+ ${PROJECT_NAME} STATIC
174+ $<TARGET_OBJECTS:${PROJECT_NAME} -obj>
175+ $<TARGET_OBJECTS:${PROJECT_NAME} -common>
176+ )
177+ # C++ PiCode Library v1.2 require the pure C library static as "c${PROJECT_NAME}"
178+ target_link_libraries (${PROJECT_NAME} PUBLIC c${PROJECT_NAME} )
179+
180+ # Pure C PiCode Library dynamic
181+ # ---------------------------------------------------------------------------------
182+ # Shared library built from the same object files
183+ add_library (
184+ c${PROJECT_NAME} -dynamic SHARED
185+ $<TARGET_OBJECTS:c${PROJECT_NAME} -obj>
186+ $<TARGET_OBJECTS:${PROJECT_NAME} -common>
187+ )
188+ # File extension OS depends, like: libcpicode.so or libcpicode.dylib or libcpicode.dll
189+ set_target_properties ( c${PROJECT_NAME} -dynamic PROPERTIES OUTPUT_NAME c${PROJECT_NAME} )
190+
191+ # Set version numbers for the versioned shared libraries target.
192+ # For shared libraries and executables on Windows and Mach-O systems
193+ # the SOVERSION property corresponds to the compatibility version
194+ # and VERSION corresponds to the current version
195+ #
196+ # Note that SOVERSION will still be used to form the install_name and
197+ # both SOVERSION and VERSION may also affect the file and symlink names.
198+ # Use the NAMELINK_SKIP option of the install command to prevent the
199+ # generation of the versionless library name symbolic link to the
200+ # versioned library file.
201+ set_target_properties ( c${PROJECT_NAME} -dynamic PROPERTIES
202+ SOVERSION ${SO_VERSION}
203+ VERSION ${CU_VERSION}
204+ )
205+
206+ # Pure C PiCode Library static
207+ # File extension OS depends, like: libcpicode.a or libcpicode.lib
208+ add_library (
209+ c${PROJECT_NAME} STATIC
210+ $<TARGET_OBJECTS:c${PROJECT_NAME} -obj>
211+ $<TARGET_OBJECTS:${PROJECT_NAME} -common>
212+ )
139213
140214# Add install targets
141215install (TARGETS ${PROJECT_NAME} DESTINATION lib)
142216install (TARGETS ${PROJECT_NAME} -dynamic DESTINATION lib)
143217
218+ # Pure C PiCode Library install targets
219+ # ---------------------------------------------------------------------------------
220+ install (TARGETS c${PROJECT_NAME} DESTINATION lib)
221+ install (TARGETS c${PROJECT_NAME} -dynamic DESTINATION lib)
222+
144223# If no has parent directory, add uninstall targets
145224if (NOT hasParent)
146225 MESSAGE (STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX} " )
@@ -149,15 +228,50 @@ if(NOT hasParent)
149228 )
150229endif ()
151230
231+ # Examples
232+ # ---------------------------------------------------------------------------------
233+ # Checking for math library 'libm' used when including <math.h> in pilight sources
234+ find_library (MATH_LIBRARY NAMES m)
235+ if (MATH_LIBRARY)
236+ MESSAGE ( STATUS "Math library: " ${MATH_LIBRARY} )
237+ else ()
238+ if (NOT MSVC )
239+ message (FATAL_ERROR "Cannot find math library 'libm' " )
240+ else ()
241+ set (MATH_LIBRARY "" )
242+ endif ()
243+ endif ()
244+
152245# Add picode_example source file, link static, no build as default
153246add_executable ( picode_example picode_example.cpp )
154- target_link_libraries ( picode_example PRIVATE ${PROJECT_NAME} )
247+ target_link_libraries ( picode_example PRIVATE ${PROJECT_NAME} ${MATH_LIBRARY} )
155248set_target_properties ( picode_example PROPERTIES EXCLUDE_FROM_ALL TRUE )
156249
250+ # Add executable export symbols for loadable modules to prevent policy CMP0065 warning
251+ set_property (TARGET picode_example PROPERTY ENABLE_EXPORTS 1)
252+
157253# If git info available adds to picode_example executable as environment var
158254if (DEFINED BUILD_VERSION)
159255 target_compile_definitions ( picode_example PRIVATE BUILD_VERSION=${BUILD_VERSION} )
160256endif (DEFINED BUILD_VERSION)
161257
162258# Add complier identification to picode_example executable as environment var
163259target_compile_definitions ( picode_example PRIVATE BUILD_COMPILER=${BUILD_COMPILER} )
260+
261+ # Pure C PiCode Library example
262+ # ---------------------------------------------------------------------------------
263+ # Add cpicode_example source file, link static, no build as default
264+ add_executable ( cpicode_example cpicode_example.c )
265+ target_link_libraries ( cpicode_example PRIVATE c${PROJECT_NAME} ${MATH_LIBRARY} )
266+ set_target_properties ( cpicode_example PROPERTIES EXCLUDE_FROM_ALL TRUE )
267+
268+ # Add executable export symbols for loadable modules to prevent policy CMP0065 warning
269+ set_property (TARGET cpicode_example PROPERTY ENABLE_EXPORTS 1)
270+
271+ # If git info available adds to cpicode_example executable as environment var
272+ if (DEFINED BUILD_VERSION)
273+ target_compile_definitions ( cpicode_example PRIVATE BUILD_VERSION=${BUILD_VERSION} )
274+ endif (DEFINED BUILD_VERSION)
275+
276+ # Add complier identification to cpicode_example executable as environment var
277+ target_compile_definitions ( cpicode_example PRIVATE BUILD_COMPILER=${BUILD_COMPILER} )
0 commit comments