Salome HOME
Porting: sip 5
[tools/configuration.git] / cmake / UseSIP.cmake
1 # Copyright (C) 2012-2020  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 # Author: Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
20
21 ####################################################################
22 #
23 # SIP_WRAP_SIP macro
24 #
25 # Generate C++ wrappings for *.sip files by processing them with sip.
26 #
27 # USAGE: SIP_WRAP_SIP(output_files sip_file [sip_file...] [OPTIONS options] [SOURCES sources])
28 #
29 # ARGUMENTS:
30 #   output_files [out] variable where output file names are listed to
31 #   sip_file     [in]  input sip file (a sequence can be provided)
32 #   options      [in]  additional options to be specified to sip
33 #   sources      [in]  additional source files to be included into output (see below)
34
35 # NOTES:
36 #   - Input files are considered relative to the current source directory.
37 #   - Output files are generated in the current build directory.
38
39 # WARNING:
40 #   - Macro requires class(es) definition in the *.sip file(s) to be started
41 #     on a new line without any preceeding characters.
42 #   - Macro does not properly processes sip features which are wrapped
43 #     with sip conditionals.
44 #   - Macro works correctly only if one single sip module is processed
45 #     (there's only one %Module directive within the set of input sip files).
46 #   - Macro sometimes does not correctly computes full set of source files
47 #     generated by sip; SOURCES option can be used to specify additional source
48 #     files.
49 #
50 # TODO:
51 #   - Check if dependency of static sources on generated headers works properly:
52 #     if header is changed, dependant sources should be recompiled.
53 #   - Process sip conditionals.
54 #   - Process several sip modules.
55
56 ####################################################################
57 MACRO(SIP_WRAP_SIP outfiles)
58   SET(_output)
59   SET(_src_ext ".cc")
60   SET(_options -s ${_src_ext} -c .)
61   SET(_sip_files)
62   SET(_get_options "0")
63   SET(_get_sources "0")
64   FOREACH(_input ${ARGN})
65     IF(${_input} STREQUAL "OPTIONS")
66       SET(_get_options "1")
67       SET(_get_sources "0")
68     ELSEIF(${_input} STREQUAL "SOURCES")
69       SET(_get_sources "1")
70       SET(_get_options "0")
71     ELSE()
72       IF(${_get_options} STREQUAL "1")
73         SET(_options ${_options} ${_input})
74       ELSEIF(${_get_sources} STREQUAL "1")
75         LIST(APPEND _output ${CMAKE_CURRENT_BINARY_DIR}/${_input})
76         SET(${outfiles} ${${outfiles}} ${CMAKE_CURRENT_BINARY_DIR}/${_input})
77       ELSE()
78         SET(_sip_files ${_sip_files} ${_input})
79       ENDIF()
80     ENDIF()
81   ENDFOREACH()
82   SET(_module_input)
83   FOREACH(_input ${_sip_files})
84     FILE(STRINGS ${_input} _sip_modules REGEX "%Module( |\\()")
85     FILE(STRINGS ${_input} _sip_classes REGEX "^class ")
86     FOREACH(_sip_module ${_sip_modules})
87       STRING(REGEX MATCH ".*%Module *\\( *name=.*\\).*" _mod_name "${_sip_module}")
88       IF (_mod_name)
89         STRING(REGEX REPLACE ".*%Module *\\( *name=(.*).*\\).*" "\\1" _mod_name ${_sip_module})
90       ELSE()
91         STRING(REGEX REPLACE ".*%Module *(.*)" "\\1" _mod_name ${_sip_module})
92       ENDIF()
93       SET(_mod_header "sipAPI${_mod_name}.h")
94       SET(_mod_source "sip${_mod_name}cmodule${_src_ext}")
95       LIST(APPEND _output ${CMAKE_CURRENT_BINARY_DIR}/${_mod_source})
96       SET(${outfiles} ${${outfiles}} ${CMAKE_CURRENT_BINARY_DIR}/${_mod_source})
97       SET(_module_input ${_input})
98     ENDFOREACH()
99     FOREACH(_sip_class ${_sip_classes})
100       STRING(REGEX MATCH ".*class +.* *:" _class_name "${_sip_class}")
101       IF (_class_name)
102         STRING(REGEX REPLACE ".*class +(.*) *:.*" "\\1" _class_name ${_sip_class})
103       ELSE()
104         STRING(REGEX REPLACE ".*class *(.*)" "\\1" _class_name ${_sip_class})
105       ENDIF()
106       STRING(STRIP ${_class_name} _class_name)
107       SET(_class_source "sip${_mod_name}${_class_name}${_src_ext}")
108       LIST(APPEND _output ${CMAKE_CURRENT_BINARY_DIR}/${_class_source})
109       SET(${outfiles} ${${outfiles}} ${CMAKE_CURRENT_BINARY_DIR}/${_class_source})
110     ENDFOREACH()
111   ENDFOREACH()
112   IF(SIP_VERSION AND SIP_VERSION VERSION_GREATER_EQUAL "5")
113     LIST(GET _sip_files 0 _main_sip_file)
114     ADD_CUSTOM_COMMAND(
115       OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/sip.h
116       COMMAND ${SIP_MODULE_EXECUTABLE} --sip-h --target-dir ${CMAKE_CURRENT_BINARY_DIR} ${_main_sip_file}
117       DEPENDS ${_sip_files}
118       )
119     SET(_extra_deps ${CMAKE_CURRENT_BINARY_DIR}/sip.h)
120   ENDIF()
121   ADD_CUSTOM_COMMAND(
122     OUTPUT ${_output}
123     COMMAND ${SIP_EXECUTABLE} ${_options} ${CMAKE_CURRENT_SOURCE_DIR}/${_module_input}
124     DEPENDS ${_module_input} ${_extra_deps}
125     )
126 ENDMACRO(SIP_WRAP_SIP)