CMakeLists.txt 10.8 KB
Newer Older
1
##
Matthieu Kermagoret's avatar
Matthieu Kermagoret committed
2
## Copyright 2011-2014 Merethis
3
##
4
## This file is part of Centreon Perl Connector.
5
##
6
## Centreon Perl Connector is free software: you can redistribute it
7
8
9
10
## and/or modify it under the terms of the GNU Affero General Public
## License as published by the Free Software Foundation, either version
## 3 of the License, or (at your option) any later version.
##
11
## Centreon Perl Connector is distributed in the hope that it will be
12
13
14
15
16
## useful, but WITHOUT ANY WARRANTY; without even the implied warranty
## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## Affero General Public License for more details.
##
## You should have received a copy of the GNU Affero General Public
17
## License along with Centreon Perl Connector. If not, see
18
19
20
21
22
## <http://www.gnu.org/licenses/>.
##

# Global options.
cmake_minimum_required(VERSION 2.8)
23
project("Centreon Perl Connector" C CXX)
24
set(PROJECT_SOURCE_DIR "${PROJECT_SOURCE_DIR}/..")
25
set(INC_DIR "${PROJECT_SOURCE_DIR}/inc/com/centreon/connector/perl")
26
set(SRC_DIR "${PROJECT_SOURCE_DIR}/src")
27
set(TEST_DIR "${PROJECT_SOURCE_DIR}/test")
28
include_directories("${PROJECT_SOURCE_DIR}/inc")
29

30
# Project version.
Dorian Guillois's avatar
Dorian Guillois committed
31
set(CONNECTOR_PERL_MAJOR 1)
32
33
set(CONNECTOR_PERL_MINOR 1)
set(CONNECTOR_PERL_PATCH 0)
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
set(CONNECTOR_PERL_VERSION "${CONNECTOR_PERL_MAJOR}.${CONNECTOR_PERL_MINOR}.${CONNECTOR_PERL_PATCH}")
add_definitions(-DCENTREON_CONNECTOR_PERL_VERSION=\"${CONNECTOR_PERL_VERSION}\")

# We will use pkg-config if available.
include(FindPkgConfig)
if (PKG_CONFIG_FOUND)
  pkg_check_modules(CLIB QUIET "centreon-clib") # Will be used below.
endif ()

# Find Centreon Clib's headers.
if (WITH_CENTREON_CLIB_INCLUDE_DIR)
  find_file(
    CLIB_HEADER_FOUND
    "com/centreon/clib/version.hh"
    PATHS "${WITH_CENTREON_CLIB_INCLUDE_DIR}"
    NO_DEFAULT_PATH)
  if (NOT CLIB_HEADER_FOUND)
    message(FATAL_ERROR "Could not find Centreon Clib's headers in ${WITH_CENTREON_CLIB_INCLUDE_DIR}.")
  endif ()
  include_directories("${WITH_CENTREON_CLIB_INCLUDE_DIR}")
54
  set(CLIB_INCLUDE_DIR "${WITH_CENTREON_CLIB_INCLUDE_DIR}")
55
56
57
58
59
60
61
elseif (CLIB_FOUND) # Was Centreon Clib detected with pkg-config ?
  if (CMAKE_CXX_FLAGS)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CLIB_CFLAGS}")
  else ()
    set(CMAKE_CXX_FLAGS "${CLIB_CFLAGS}")
  endif ()
else ()
Dorian Guillois's avatar
Dorian Guillois committed
62
  find_path(CLIB_INCLUDE_DIR "com/centreon/clib/version.hh" PATH_SUFFIXES "centreon-clib")
63
  if (NOT CLIB_INCLUDE_DIR)
Dorian Guillois's avatar
Dorian Guillois committed
64
    message(FATAL_ERROR "Could not find Centreon Clib's headers (try WITH_CENTREON_CLIB_INCLUDE_DIR).")
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  endif ()
  include_directories("${CLIB_INCLUDE_DIR}")
endif ()

# Find Centreon Clib's library.
if (WITH_CENTREON_CLIB_LIBRARIES)
  set(CLIB_LIBRARIES "${WITH_CENTREON_CLIB_LIBRARIES}")
elseif (WITH_CENTREON_CLIB_LIBRARY_DIR)
  find_library(
    CLIB_LIBRARIES
    "centreon_clib"
    PATHS "${WITH_CENTREON_CLIB_LIBRARY_DIR}"
    NO_DEFAULT_PATH)
  if (NOT CLIB_LIBRARIES)
    message(FATAL_ERROR "Could not find Centreon Clib's library in ${WITH_CENTREON_CLIB_LIBRARY_DIR}.")
  endif ()
elseif (CLIB_FOUND) # Was Centreon Clib detected with pkg-config ?
  set(CLIB_LIBRARIES "${CLIB_LDFLAGS}")
else ()
  find_library(CLIB_LIBRARIES "centreon_clib")
  if (NOT CLIB_LIBRARIES)
    message(FATAL_ERROR "Could not find Centreon Clib's library (try WITH_CENTREON_CLIB_LIBRARY_DIR or WITH_CENTREON_CLIB_LIBRARIES).")
  endif ()
endif ()

90
91
92
93
94
# Find Perl.
include(FindPerlLibs)
if (NOT PERLLIBS_FOUND)
  message(FATAL_ERROR "Could not find Perl libraries.")
endif ()
95
96
execute_process(COMMAND "${PERL_EXECUTABLE}" "-MExtUtils::Embed" "-e" "ldopts"
  RESULT_VARIABLE PERL_LDFLAGS_ERROR
97
98
  OUTPUT_VARIABLE PERL_LIBRARIES)
string(STRIP "${PERL_LIBRARIES}" PERL_LIBRARIES)
99
100
101
if (PERL_LDFLAGS_ERROR)
  set(PERL_LIBRARIES ${PERL_LIBRARY})
endif ()
102
103
104
105
execute_process(COMMAND "${PERL_EXECUTABLE}" "-MExtUtils::Embed" "-e" "ccopts"
  RESULT_VARIABLE PERL_CFLAGS_ERROR
  OUTPUT_VARIABLE PERL_CFLAGS)
if (NOT PERL_CFLAGS_ERROR)
Dorian Guillois's avatar
Dorian Guillois committed
106
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PERL_CFLAGS}")
107
endif ()
108

109
110
111
112
# Generate code to dynamically load modules.
add_custom_command(OUTPUT "${SRC_DIR}/xs_init.cc"
  COMMAND "${PERL_EXECUTABLE}" "-MExtUtils::Embed" "-e" "xsinit" "--" "-o" "${SRC_DIR}/xs_init.cc")

113
114
115
116
117
118
119
120
121
122
123
# Disable some warnings generated by Embedded Perl.
get_property(EMBEDDED_PERL_CXXFLAGS
  SOURCE "${SRC_DIR}/embedded_perl.cc"
  PROPERTY COMPILE_FLAGS)
if (EMBEDDED_PERL_CXXFLAGS)
  string(REGEX REPLACE "-pedantic *"
    EMBEDDED_PERL_CXXFLAGS "${EMBEDDED_PERL_CXXFLAGS}")
  set_property(SOURCE "${SRC_DIR}/embedded_perl.cc"
    PROPERTY COMPILE_FLAGS "${EMBEDDED_PERL_CXXFLAGS}")
endif ()

124
125
# Perl connector library.
set(CONNECTORLIB "centreonconnectorperl")
126
add_library("${CONNECTORLIB}" STATIC
127
  # Sources.
128
  "${SRC_DIR}/checks/check.cc"
129
  "${SRC_DIR}/checks/listener.cc"
130
  "${SRC_DIR}/checks/result.cc"
131
  "${SRC_DIR}/checks/timeout.cc"
132
  "${SRC_DIR}/embedded_perl.cc"
133
  "${SRC_DIR}/multiplexer.cc"
134
  "${SRC_DIR}/options.cc"
135
136
  "${SRC_DIR}/orders/listener.cc"
  "${SRC_DIR}/orders/parser.cc"
137
  "${SRC_DIR}/pipe_handle.cc"
138
  "${SRC_DIR}/policy.cc"
139
  "${SRC_DIR}/reporter.cc"
140
  "${SRC_DIR}/script.cc"
141
  "${SRC_DIR}/xs_init.cc"
142
  # Headers.
143
  "${INC_DIR}/checks/check.hh"
144
  "${INC_DIR}/checks/listener.hh"
145
  "${INC_DIR}/checks/result.hh"
146
  "${INC_DIR}/checks/timeout.hh"
147
  "${INC_DIR}/embedded_perl.hh"
148
  "${INC_DIR}/multiplexer.hh"
149
  "${INC_DIR}/namespace.hh"
150
  "${INC_DIR}/options.hh"
151
152
  "${INC_DIR}/orders/listener.hh"
  "${INC_DIR}/orders/parser.hh"
153
  "${INC_DIR}/pipe_handle.hh"
154
  "${INC_DIR}/policy.hh"
155
  "${INC_DIR}/reporter.hh"
156
157
158
)
target_link_libraries(
  "${CONNECTORLIB}"
159
  ${PERL_LIBRARIES}
160
  ${CLIB_LIBRARIES})
161
162
163
164
165

# Perl connector.
set(CONNECTOR "centreon_connector_perl")
add_executable("${CONNECTOR}"
  # Sources.
166
167
168
169
170
171
172
173
174
175
  "${SRC_DIR}/main.cc")
target_link_libraries("${CONNECTOR}" ${CONNECTORLIB})

# Installation path.
if (WITH_PREFIX)
  set(CMAKE_INSTALL_PREFIX "${WITH_PREFIX}")
endif ()
if (WITH_PREFIX_BINARY)
  set(PREFIX_BINARY "${WITH_PREFIX_BINARY}")
else ()
176
  set(PREFIX_BINARY "${CMAKE_INSTALL_PREFIX}/bin")
177
178
179
180
181
182
183
184
endif ()

# Install rules.
install(TARGETS "${CONNECTOR}"
  RUNTIME DESTINATION "${PREFIX_BINARY}")

# Packaging.
include("package.cmake")
185
186
187
188
189
190

# Testing.
option(WITH_TESTING "Build unit tests." OFF)
if (WITH_TESTING)
  # Enable testing.
  enable_testing()
191
  include_directories("${PROJECT_SOURCE_DIR}")
192
  set(CONNECTOR_TEST_DIR "${PROJECT_SOURCE_DIR}/test/connector")
193
194
195
196
  get_property(CONNECTOR_PERL_BINARY
    TARGET "${CONNECTOR}"
    PROPERTY LOCATION)
  configure_file(
197
198
    "${TEST_DIR}/connector/paths.hh.in"
    "${TEST_DIR}/connector/paths.hh")
199

200
  # Embedded Perl tests.
201
202
  #   Simple script execution #1.
  set(TEST_NAME "embedded_perl_run_simple_1")
203
  add_executable("${TEST_NAME}"
204
205
206
207
208
209
210
    "${TEST_DIR}/embedded_perl/run_simple_1.cc")
  target_link_libraries("${TEST_NAME}" ${CONNECTORLIB})
  add_test("${TEST_NAME}" "${TEST_NAME}")
  #   Simple script execution #2.
  set(TEST_NAME "embedded_perl_run_simple_2")
  add_executable("${TEST_NAME}"
    "${TEST_DIR}/embedded_perl/run_simple_2.cc")
211
212
213
  target_link_libraries("${TEST_NAME}" ${CONNECTORLIB})
  add_test("${TEST_NAME}" "${TEST_NAME}")

214
215
216
217
218
219
220
221
222
223
224
225

  #
  # Process tests.
  #
  # Help.
  add_test("connector_help" "${CONNECTOR}" "--help")
  # Version.
  add_test("connector_version" "${CONNECTOR}" "--version")
  # Invalid flag.
  add_test("connector_invalid_flag" "${CONNECTOR}" "--merethis")
  set_tests_properties("connector_invalid_flag"
    PROPERTIES WILL_FAIL TRUE)
226
227
228
229
230
  # Execution library.
  add_library("test_execute" STATIC
    "${TEST_DIR}/connector/misc.cc"
    "${TEST_DIR}/connector/misc.hh")
  set(TEST_LIBRARIES ${CLIB_LIBRARIES} "test_execute")
231
232
233
234
235
236
237
238
239
240
  # EOF on stdin.
  set(TEST_NAME "connector_eof_on_stdin")
  add_executable("${TEST_NAME}"
    "${TEST_DIR}/connector/eof_on_stdin.cc")
  target_link_libraries("${TEST_NAME}" ${CLIB_LIBRARIES})
  add_test("${TEST_NAME}" "${TEST_NAME}")
  # Non-existent script.
  set(TEST_NAME "connector_non_existent_script")
  add_executable("${TEST_NAME}"
    "${TEST_DIR}/connector/non_existent_script.cc")
241
  target_link_libraries("${TEST_NAME}" ${TEST_LIBRARIES})
242
  add_test("${TEST_NAME}" "${TEST_NAME}")
243
244
245
246
  # Graceful timeout.
  set(TEST_NAME "connector_timeout_term")
  add_executable("${TEST_NAME}"
    "${TEST_DIR}/connector/timeout_term.cc")
247
  target_link_libraries("${TEST_NAME}" ${TEST_LIBRARIES})
248
249
250
251
252
  add_test("${TEST_NAME}" "${TEST_NAME}")
  # Forced timeout.
  set(TEST_NAME "connector_timeout_kill")
  add_executable("${TEST_NAME}"
    "${TEST_DIR}/connector/timeout_kill.cc")
253
  target_link_libraries("${TEST_NAME}" ${TEST_LIBRARIES})
254
  add_test("${TEST_NAME}" "${TEST_NAME}")
255
256
257
258
259
260
261
262
263
264
265
266
  # Single script execution.
  set(TEST_NAME "connector_execute_single_script")
  add_executable("${TEST_NAME}"
    "${TEST_DIR}/connector/execute_single_script.cc")
  target_link_libraries("${TEST_NAME}" ${TEST_LIBRARIES})
  add_test("${TEST_NAME}" "${TEST_NAME}")
  # Multiple script execution.
  set(TEST_NAME "connector_execute_multiple_scripts")
  add_executable("${TEST_NAME}"
    "${TEST_DIR}/connector/execute_multiple_scripts.cc")
  target_link_libraries("${TEST_NAME}" ${TEST_LIBRARIES})
  add_test("${TEST_NAME}" "${TEST_NAME}")
267
268
269
270
271
272
  # Additional code.
  set(TEST_NAME "connector_execute_with_additional_code")
  add_executable("${TEST_NAME}"
    "${TEST_DIR}/connector/execute_with_additional_code.cc")
  target_link_libraries("${TEST_NAME}" ${TEST_LIBRARIES})
  add_test("${TEST_NAME}" "${TEST_NAME}")
273
274
275
276
277
278
  # Module loading from script.
  set(TEST_NAME "connector_execute_module_loading")
  add_executable("${TEST_NAME}"
    "${TEST_DIR}/connector/execute_module_loading.cc")
  target_link_libraries("${TEST_NAME}" ${TEST_LIBRARIES})
  add_test("${TEST_NAME}" "${TEST_NAME}")
279
280
281
282
283
284
285
286
287

endif ()

# Summary.
message(STATUS "")
message(STATUS "Configuration Summary")
message(STATUS "---------------------")
message(STATUS "")
message(STATUS "  Project")
288
message(STATUS "    - Name                       Centreon Perl Connector")
289
290
291
292
293
294
295
296
297
298
message(STATUS "    - Version                    ${CONNECTOR_PERL_VERSION}")
message(STATUS "")
message(STATUS "  System")
message(STATUS "    - Name                       ${CMAKE_SYSTEM_NAME}")
message(STATUS "    - Version                    ${CMAKE_SYSTEM_VERSION}")
message(STATUS "    - Processor                  ${CMAKE_SYSTEM_PROCESSOR}")
message(STATUS "")
message(STATUS "  Build")
message(STATUS "    - Compiler                   ${CMAKE_CXX_COMPILER} (${CMAKE_CXX_COMPILER_ID})")
message(STATUS "    - Extra compilation flags    ${CMAKE_CXX_FLAGS}")
299
300
301
302
303
if (WITH_TESTING)
  message(STATUS "    - Unit tests                 enabled")
else()
  message(STATUS "    - Unit tests                 disabled")
endif ()
304
305
306
307
308
309
message(STATUS "")
message(STATUS "  Install")
message(STATUS "    - Prefix                     ${CMAKE_INSTALL_PREFIX}")
message(STATUS "    - Binary prefix              ${PREFIX_BINARY}")
message(STATUS "    - Package                    ${PACKAGE_LIST}")
message(STATUS "")
310
311
312
message(STATUS "  Libraries")
message(STATUS "    - clib include directory     ${CLIB_INCLUDE_DIR}")
message(STATUS "    - clib library directory     ${CLIB_LIBRARIES}")