CMakeLists.txt 11 KB
Newer Older
1
##
2
## Copyright 2011-2015,2017 Centreon
3
##
4
5
6
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
7
##
8
##     http://www.apache.org/licenses/LICENSE-2.0
9
##
10
11
12
13
14
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
15
##
16
## For more information : contact@centreon.com
17
18
19
20
##

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

28
# Project version.
Dorian Guillois's avatar
Dorian Guillois committed
29
set(CONNECTOR_PERL_MAJOR 1)
30
set(CONNECTOR_PERL_MINOR 1)
31
set(CONNECTOR_PERL_PATCH 3)
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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}")
52
  set(CLIB_INCLUDE_DIR "${WITH_CENTREON_CLIB_INCLUDE_DIR}")
53
54
55
56
57
58
59
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
60
  find_path(CLIB_INCLUDE_DIR "com/centreon/clib/version.hh" PATH_SUFFIXES "centreon-clib")
61
  if (NOT CLIB_INCLUDE_DIR)
Dorian Guillois's avatar
Dorian Guillois committed
62
    message(FATAL_ERROR "Could not find Centreon Clib's headers (try WITH_CENTREON_CLIB_INCLUDE_DIR).")
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  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 ()

88
89
90
91
92
# Find Perl.
include(FindPerlLibs)
if (NOT PERLLIBS_FOUND)
  message(FATAL_ERROR "Could not find Perl libraries.")
endif ()
93
94
execute_process(COMMAND "${PERL_EXECUTABLE}" "-MExtUtils::Embed" "-e" "ldopts"
  RESULT_VARIABLE PERL_LDFLAGS_ERROR
95
96
  OUTPUT_VARIABLE PERL_LIBRARIES)
string(STRIP "${PERL_LIBRARIES}" PERL_LIBRARIES)
97
98
99
if (PERL_LDFLAGS_ERROR)
  set(PERL_LIBRARIES ${PERL_LIBRARY})
endif ()
100
101
102
103
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
104
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PERL_CFLAGS}")
105
endif ()
106

107
108
109
110
# 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")

111
112
113
114
115
116
117
118
119
120
121
# 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 ()

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

# Perl connector.
set(CONNECTOR "centreon_connector_perl")
add_executable("${CONNECTOR}"
  # Sources.
164
165
166
167
168
169
170
171
172
173
  "${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 ()
174
  set(PREFIX_BINARY "${CMAKE_INSTALL_PREFIX}/bin")
175
176
177
178
179
180
181
182
endif ()

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

# Packaging.
include("package.cmake")
183
184
185
186
187
188

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

198
  # Embedded Perl tests.
199
200
  #   Simple script execution #1.
  set(TEST_NAME "embedded_perl_run_simple_1")
201
  add_executable("${TEST_NAME}"
202
203
204
205
206
207
208
    "${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")
209
210
211
  target_link_libraries("${TEST_NAME}" ${CONNECTORLIB})
  add_test("${TEST_NAME}" "${TEST_NAME}")

212
213
214
215
216
217
218
219
220
221
222
223

  #
  # 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)
224
225
226
227
228
  # Execution library.
  add_library("test_execute" STATIC
    "${TEST_DIR}/connector/misc.cc"
    "${TEST_DIR}/connector/misc.hh")
  set(TEST_LIBRARIES ${CLIB_LIBRARIES} "test_execute")
229
230
231
232
233
234
235
236
237
238
  # 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")
239
  target_link_libraries("${TEST_NAME}" ${TEST_LIBRARIES})
240
  add_test("${TEST_NAME}" "${TEST_NAME}")
241
242
243
244
  # Graceful timeout.
  set(TEST_NAME "connector_timeout_term")
  add_executable("${TEST_NAME}"
    "${TEST_DIR}/connector/timeout_term.cc")
245
  target_link_libraries("${TEST_NAME}" ${TEST_LIBRARIES})
246
247
248
249
250
  add_test("${TEST_NAME}" "${TEST_NAME}")
  # Forced timeout.
  set(TEST_NAME "connector_timeout_kill")
  add_executable("${TEST_NAME}"
    "${TEST_DIR}/connector/timeout_kill.cc")
251
  target_link_libraries("${TEST_NAME}" ${TEST_LIBRARIES})
252
  add_test("${TEST_NAME}" "${TEST_NAME}")
253
254
255
256
257
258
  # 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}")
259
260
261
262
263
264
  # Single script execution with log file.
  set(TEST_NAME "connector_execute_single_script_log_file")
  add_executable("${TEST_NAME}"
    "${TEST_DIR}/connector/execute_single_script_log_file.cc")
  target_link_libraries("${TEST_NAME}" ${TEST_LIBRARIES})
  add_test("${TEST_NAME}" "${TEST_NAME}")
265
266
267
268
269
270
  # 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}")
271
272
273
274
275
276
  # 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}")
277
278
279
280
281
282
  # 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}")
283
284
285
286
287
288
289
290
291

endif ()

# Summary.
message(STATUS "")
message(STATUS "Configuration Summary")
message(STATUS "---------------------")
message(STATUS "")
message(STATUS "  Project")
292
message(STATUS "    - Name                       Centreon Perl Connector")
293
294
295
296
297
298
299
300
301
302
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}")
303
304
305
306
307
if (WITH_TESTING)
  message(STATUS "    - Unit tests                 enabled")
else()
  message(STATUS "    - Unit tests                 disabled")
endif ()
308
309
310
311
312
313
message(STATUS "")
message(STATUS "  Install")
message(STATUS "    - Prefix                     ${CMAKE_INSTALL_PREFIX}")
message(STATUS "    - Binary prefix              ${PREFIX_BINARY}")
message(STATUS "    - Package                    ${PACKAGE_LIST}")
message(STATUS "")
314
315
316
message(STATUS "  Libraries")
message(STATUS "    - clib include directory     ${CLIB_INCLUDE_DIR}")
message(STATUS "    - clib library directory     ${CLIB_LIBRARIES}")