CMakeLists.txt: use GNUInstallDirs
[libam7xxx.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 2.6)
2 project(libam7xxx C)
3
4 include (GNUInstallDirs)
5
6 set(PROJECT_DESCRIPTION
7   "Communication library for Actions Micro AM7XXX based USB projectors and DPFs")
8
9 set(PROJECT_VER_MAJOR 0)
10 set(PROJECT_VER_MINOR 1)
11 set(PROJECT_VER_PATCH 7)
12 set(PROJECT_VER_EXTRA "")
13 set(PROJECT_VER
14   "${PROJECT_VER_MAJOR}.${PROJECT_VER_MINOR}.${PROJECT_VER_PATCH}${PROJECT_VER_EXTRA}")
15 set(PROJECT_APIVER
16   "${PROJECT_VER_MAJOR}.${PROJECT_VER_MINOR}")
17
18 set(CMAKE_MODULE_PATH
19   ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/")
20
21 OPTION(STRICT_COMPILATION_CHECKS "Enable stricter compilation checks" OFF)
22
23 include (MaintenanceTools)
24
25 set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
26 set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
27 set(DOC_OUTPUT_PATH ${CMAKE_BINARY_DIR}/doc)
28
29 # Because cmake cannot deal sanely with multiline strings. SRSLY?
30 # See http://www.vtkedge.org/Bug/view.php?id=8362&nbn=8
31 macro(add_flags var)
32   string(REPLACE ";" " " _flags "${ARGN}")
33   set(${var} "${${var}} ${_flags}")
34 endmacro(add_flags)
35
36 string(REGEX MATCH "clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER}")
37
38 if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
39   add_definitions(-Wall)
40
41   # let CFLAGS env override this
42   if(CMAKE_C_FLAGS STREQUAL "")
43     set(CMAKE_C_FLAGS "-std=c99 -pedantic -Wall -Wextra")
44   endif()
45
46   add_flags(CMAKE_C_FLAGS
47     -fvisibility=hidden
48     -fno-common
49     -Wall
50     -Wextra
51     -Wformat=2
52     -Winit-self
53     -Winline
54     -Wpacked
55     -Wpointer-arith
56     -Wlarger-than-65500
57     -Wmissing-declarations
58     -Wmissing-format-attribute
59     -Wmissing-noreturn
60     -Wmissing-prototypes
61     -Wnested-externs
62     -Wold-style-definition
63     -Wredundant-decls
64     -Wsign-compare
65     -Wstrict-aliasing=2
66     -Wstrict-prototypes
67     -Wswitch-enum
68     -Wundef
69     -Wunreachable-code
70     -Wwrite-strings)
71
72   include(CheckCCompilerFlag)
73   CHECK_C_COMPILER_FLAG(-fstack-protector-strong COMPILER_SUPPORTS_STACK_PROTECTOR_STRONG)
74   if (COMPILER_SUPPORTS_STACK_PROTECTOR_STRONG)
75     add_flags(CMAKE_C_FLAGS -fstack-protector-strong)
76   else()
77     add_flags(CMAKE_C_FLAGS -fstack-protector)
78   endif()
79
80   add_flags(DEBUG_FLAGS
81     -ggdb)
82
83   add_flags(RELEASE_FLAGS
84     -Wp,-D_FORTIFY_SOURCE=2)
85
86   if (STRICT_COMPILATION_CHECKS)
87     add_flags(STRICT_FLAGS
88       -Werror
89       # sign conversion warnings can be very noisy for a very little gain
90       #-Wsign-conversion
91       # NOTE: Vanilla libusb-1.0.8 can't live with -pedantic-errors
92       -pedantic-errors)
93
94   endif()
95 endif()
96
97 if (CMAKE_COMPILER_IS_GNUCC)
98   add_flags(CMAKE_C_FLAGS
99     -Wunsafe-loop-optimizations
100     --param=ssp-buffer-size=4)
101
102   if (STRICT_COMPILATION_CHECKS)
103     add_flags(STRICT_FLAGS
104       # NOTE: GCC >= 4.6 is needed for -Wunused-but-set-variable
105       -Wunused-but-set-variable)
106   endif()
107 endif()
108
109 if (CMAKE_COMPILER_IS_CLANG)
110   if (STRICT_COMPILATION_CHECKS)
111     add_flags(STRICT_FLAGS
112       -Wshorten-64-to-32)
113   endif()
114 endif()
115
116 set(CMAKE_C_FLAGS_DEBUG "-O0 -DDEBUG=1 ${DEBUG_FLAGS} ${STRICT_FLAGS}")
117 set(CMAKE_C_FLAGS_RELEASE "-O2 ${RELEASE_FLAGS} ${STRICT_FLAGS}")
118 set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 ${RELEASE_FLAGS} ${DEBUG_FLAGS} ${STRICT_FLAGS}")
119
120 # Add library project
121 add_subdirectory(src)
122 add_subdirectory(examples)
123 add_subdirectory(doc)