]> Git Repo - serial.git/blob - serial.cmake
Fixed linking on Linux and OS X.
[serial.git] / serial.cmake
1 macro(build_serial)
2
3   ## Project Setup
4   cmake_minimum_required(VERSION 2.4.6)
5
6   if(COMMAND cmake_policy)
7       cmake_policy(SET CMP0003 NEW)
8   endif(COMMAND cmake_policy)
9
10   project(Serial)
11
12   ## Configurations
13   # Enable warnings
14   # Assuming unix means a gcc style compiler, eg. g++ or clang++.
15   IF(UNIX)
16       #set(CMAKE_CXX_FLAGS "-Wall -Weffc++ -pedantic -pedantic-errors -Wextra  -Wall -Waggregate-return -Wcast-align -Wcast-qual  -Wchar-subscripts  -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal  -Wformat  -Wformat=2 -Wformat-nonliteral -Wformat-security  -Wformat-y2k -Wimplicit  -Wimport  -Winit-self  -Winline -Winvalid-pch   -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute   -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses  -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point  -Wshadow -Wsign-compare  -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch  -Wswitch-default -Wswitch-enum -Wtrigraphs  -Wuninitialized -Wunknown-pragmas  -Wunreachable-code -Wunused -Wunused-function  -Wunused-label  -Wunused-parameter -Wunused-value  -Wunused-variable  -Wvariadic-macros -Wvolatile-register-var  -Wwrite-strings")
17   ELSEIF(WIN32)
18     # Force to always compile with W4
19     if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
20       string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
21     else()
22       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
23     endif()
24   endif(UNIX)
25
26   IF(NOT DEFINED BUILD_NO_CLANG)
27     option(BUILD_NO_CLANG "Do not use the Clang compiler." OFF)
28   ENDIF(NOT DEFINED BUILD_NO_CLANG)
29
30   # Use clang if available
31   IF(EXISTS /usr/bin/clang AND NOT BUILD_NO_CLANG)
32     set(CMAKE_CXX_COMPILER /usr/bin/clang++)
33     set(CMAKE_OSX_DEPLOYMENT_TARGET "")
34     set(SERIAL_BUILD_WARNINGS TRUE)
35     set(CMAKE_BUILD_TYPE Debug)
36   ENDIF(EXISTS /usr/bin/clang AND NOT BUILD_NO_CLANG)
37
38   option(SERIAL_BUILD_TESTS "Build all of the Serial tests." OFF)
39   option(SERIAL_BUILD_EXAMPLES "Build all of the Serial examples." OFF)
40
41   # Allow for building shared libs override
42   IF(NOT BUILD_SHARED_LIBS)
43       set(BUILD_SHARED_LIBS OFF)
44   ENDIF(NOT BUILD_SHARED_LIBS)
45
46   # Threading libraries added for mutexs
47   FIND_PACKAGE (Threads)
48
49   # Set the default path for built executables to the "bin" directory
50   IF(NOT DEFINED(EXECUTABLE_OUTPUT_PATH))
51       set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
52   ENDIF(NOT DEFINED(EXECUTABLE_OUTPUT_PATH))
53   # set the default path for built libraries to the "lib" directory
54   IF(NOT DEFINED(LIBRARY_OUTPUT_PATH))
55       set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
56   ENDIF(NOT DEFINED(LIBRARY_OUTPUT_PATH))
57
58   ## Configure the build system
59
60   # Add the include folder to the include path
61   include_directories(${PROJECT_SOURCE_DIR}/include)
62
63   # Add default source files
64   set(SERIAL_SRCS src/serial.cc)
65   IF(WIN32)
66     list(APPEND SERIAL_SRCS src/impl/win.cc)
67   ELSE(WIN32)
68     list(APPEND SERIAL_SRCS src/impl/unix.cc)
69   ENDIF(WIN32)
70   # Add default header files
71   set(SERIAL_HEADERS include/serial/serial.h)
72
73
74   set(OTHER_LIBS "")
75   if(UNIX)
76     set(OTHER_LIBS util)
77   endif(UNIX)
78
79   if(UNIX AND NOT APPLE)
80     list(APPEND OTHER_LIBS rt pthread)
81   endif(UNIX AND NOT APPLE)
82   
83   ## Build the Serial Library
84
85   # Compile the Library
86   add_library(serial ${SERIAL_SRCS})
87   target_link_libraries(serial ${CMAKE_THREAD_LIBS_INIT} ${OTHER_LIBS})
88
89   ## Build Examples
90
91   # If asked to
92   IF(SERIAL_BUILD_EXAMPLES)
93       # Compile the Serial Test program
94       add_executable(serial_example examples/serial_example.cc)
95       # Link the Test program to the Serial library
96       target_link_libraries(serial_example serial)
97   ENDIF(SERIAL_BUILD_EXAMPLES)
98
99   ## Build tests
100
101   # If asked to
102   IF(SERIAL_BUILD_TESTS)
103       # Find Google Test
104       enable_testing()
105       find_package(GTest REQUIRED)
106       include_directories(${GTEST_INCLUDE_DIRS})
107
108       # Compile the Serial Test program
109       add_executable(serial_tests tests/serial_tests.cc)
110       # Link the Test program to the serial library
111       target_link_libraries(serial_tests ${GTEST_BOTH_LIBRARIES}
112                             serial)
113
114       add_test(AllTestsIntest_serial serial_tests)
115   ENDIF(SERIAL_BUILD_TESTS)
116
117   ## Setup install and uninstall
118
119   # Unless asked not to...
120   IF(NOT SERIAL_DONT_CONFIGURE_INSTALL)
121       # Configure make install
122       IF(NOT CMAKE_INSTALL_PREFIX)
123           SET(CMAKE_INSTALL_PREFIX /usr/local)
124       ENDIF(NOT CMAKE_INSTALL_PREFIX)
125     
126       INSTALL(TARGETS serial
127         RUNTIME DESTINATION bin
128         LIBRARY DESTINATION lib
129         ARCHIVE DESTINATION lib
130       )
131     
132       INSTALL(FILES include/serial/serial.h
133                     include/serial/v8stdint.h
134               DESTINATION include/serial)
135     
136       IF(NOT CMAKE_FIND_INSTALL_PATH)
137           set(CMAKE_FIND_INSTALL_PATH ${CMAKE_ROOT})
138       ENDIF(NOT CMAKE_FIND_INSTALL_PATH)
139     
140       INSTALL(FILES Findserial.cmake
141               DESTINATION ${CMAKE_FIND_INSTALL_PATH}/Modules/)
142     
143       ADD_CUSTOM_TARGET(uninstall @echo uninstall package)
144     
145       IF (UNIX)
146         ADD_CUSTOM_COMMAND(
147           COMMENT "uninstall package"
148           COMMAND xargs ARGS rm < install_manifest.txt
149         
150           TARGET  uninstall
151         )
152       ELSE(UNIX)
153         ADD_CUSTOM_COMMAND(
154           COMMENT "uninstall only implemented in unix"
155           TARGET  uninstall
156         )
157       ENDIF(UNIX)
158   ENDIF(NOT SERIAL_DONT_CONFIGURE_INSTALL)
159 endmacro(build_serial)
This page took 0.038959 seconds and 4 git commands to generate.