Salome HOME
CMake: modifying OMNIORB_ADD_MODULE macro to support dynamic build of IDL files
[modules/yacs.git] / salome_adm / cmake_files / UseOmniORB.cmake
1 # Copyright (C) 2007-2013  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.
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
20 INSTALL( CODE "
21 SET(INSTALL_PYIDL_DIR lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/site-packages/salome) # R1 CHECK
22 IF(WIN32)
23   SET(INSTALL_PYIDL_DIR bin/salome) # R1 CHECK
24 ENDIF(WIN32)
25
26 MACRO(OMNIORB_COMPILE_IDL_FORPYTHON_ON_INSTALL MYOMNIORBIDLPYTHON MYIDLPYFLAGS MYIDLFILE MYFULLDIR)
27   FILE(MAKE_DIRECTORY \${MYFULLDIR})
28   STRING(REPLACE \" \" \";\" MYIDLPYFLAGS2 \${MYIDLPYFLAGS})
29   MESSAGE(STATUS \"Compiling \${MYIDLFILE} into \${MYFULLDIR}\")
30   EXECUTE_PROCESS(COMMAND \${MYOMNIORBIDLPYTHON} \${MYIDLPYFLAGS2} -C\${MYFULLDIR} \${MYIDLFILE})
31 ENDMACRO(OMNIORB_COMPILE_IDL_FORPYTHON_ON_INSTALL)
32 ")
33
34 #----------------------------------------------------------------------------
35 # OMNIORB_ADD_MODULE macro: generate CORBA wrappings for a module.
36 #
37 # USAGE: OMNIORB_ADD_MODULE(module idlfiles incdirs [linklibs])
38 #
39 # ARGUMENTS:
40 #   module    : module name
41 #   idlfiles  : list of IDL files to be compiled into module. If just a file name is given, the source
42 #               tree is first inspected. If not found there, the macro assumes the file will be built
43 #               in the build tree (CMAKE_CURRENT_BINARY_DIR) thanks to some ADD_CUSTOM_COMMAND() call
44 #               (used in PARAVIS).
45 #   incdirs   : additional include dirs for IDL staff
46 #   linklibs  : additional libraries the module to be linked to (optional)
47 #
48 # For example, to build CORBA staff from MyModule.idl for module MyModule
49 # (that depends only on SALOME KERNEL interfaces), use the following code:
50 #
51 #   INCLUDE(UseOmniORB)
52 #   INCLUDE_DIRECTORIES(${OMNIORB_INCLUDE_DIR} ${KERNEL_INCLUDE_DIRS})
53 #   OMNIORB_ADD_MODULE(SalomeIDLMyModule MyModule.idl ${KERNEL_ROOT_DIR}/idl/salome ${KERNEL_SalomeIDLKernel})
54 #   INSTALL(TARGETS SalomeIDLMyModule EXPORT ${PROJECT_NAME}TargetGroup DESTINATION ${SALOME_INSTALL_LIBS})
55
56 # This macro uses the following variables:
57 # - From FindOmniORB.cmake
58 #     OMNIORB_IDL            : the path to the omniidl tool
59 #     OMNIORB_IDLCXXFLAGS    : the options to give to omniidl generator for C++ backend
60 #     OMNIORB_DEFINITIONS    : additional compile options for C++
61 # - From FindOmniORBPy.cmake
62 #     OMNIORB_IDLPYFLAGS     : the options to give to omniidl generator for Python backend
63 #     OMNIORB_PYTHON_BACKEND : Python backend
64 #
65 # TODO:
66 #   1. Replace hardcoded dirpaths bin/salome, idl/salome, etc by corresponding configuration options.
67 #   2. Revise/improve OMNIORB_COMPILE_IDL_FORPYTHON_ON_INSTALL macro usage.
68 #   3. Add proper handling of INCLUDE_DIRECTORIES to minimize this macro usage in target CMakeLists.txt files.
69 #
70 #----------------------------------------------------------------------------
71 MACRO(OMNIORB_ADD_MODULE module idlfiles incdirs)
72   # process additional libraries the module to be linked to
73   SET(_linklibs ${OMNIORB_LIBRARIES})
74   FOREACH(_arg ${ARGN})
75     SET(_linklibs ${_linklibs} ${_arg})
76   ENDFOREACH()
77   
78   # module sources
79   SET(_sources)
80   # type of the libraries: SHARED for Linux, STATIC for Windows
81   SET(_type SHARED)
82   IF(WIN32)
83     SET(_type STATIC)
84   ENDIF()
85   IF(NOT WIN32 AND (_type STREQUAL STATIC)) 
86     SET(CMAKE_POSITION_INDEPENDENT_CODE ON)
87   ENDIF()
88
89   # add additional include dirs to the C++ and Python backend options
90   SET(_cxx_flags ${OMNIORB_IDLCXXFLAGS})
91   SET(_py_flags "${OMNIORB_IDLPYFLAGS}")
92   FOREACH(_f ${incdirs})
93     LIST(APPEND _cxx_flags "-I${_f}")
94     LIST(APPEND _py_flags  "-I${_f}")
95   ENDFOREACH()
96
97   FOREACH(_input ${idlfiles})
98     GET_FILENAME_COMPONENT(_base ${_input} NAME_WE)
99     GET_FILENAME_COMPONENT(_path ${_input} PATH)
100     IF(NOT _path)
101       IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_input})
102         SET(_input_cmd ${CMAKE_CURRENT_SOURCE_DIR}/${_input})
103       ELSE()
104         SET(_input_cmd ${CMAKE_CURRENT_BINARY_DIR}/${_input})
105       ENDIF()
106     ELSE()
107       SET(_input_cmd ${_input})
108     ENDIF()
109
110     SET(_inc     ${CMAKE_CURRENT_BINARY_DIR}/${_base}.hh)
111     SET(_src     ${CMAKE_CURRENT_BINARY_DIR}/${_base}SK.cc)
112     SET(_dynsrc  ${CMAKE_CURRENT_BINARY_DIR}/${_base}DynSK.cc)
113
114     LIST(APPEND _sources ${_src})
115     LIST(APPEND _sources ${_dynsrc})
116     SET(_outputs ${_inc} ${_src} ${_dynsrc})
117
118     ADD_CUSTOM_COMMAND(OUTPUT ${_outputs}
119       COMMAND ${OMNIORB_IDL_COMPILER} ${_cxx_flags} ${_input_cmd}
120       DEPENDS ${_input})
121     
122     INSTALL(FILES ${_input} DESTINATION idl/salome)
123     INSTALL(FILES ${_inc}   DESTINATION include/salome)
124
125     IF(OMNIORB_PYTHON_BACKEND)
126       STRING(REPLACE ";" " " _tmp "${_py_flags}")
127       INSTALL(CODE "OMNIORB_COMPILE_IDL_FORPYTHON_ON_INSTALL( \"${OMNIORB_IDL_COMPILER}\" \"${_tmp}\" \"${_input}\" \"${CMAKE_INSTALL_PREFIX}/\${INSTALL_PYIDL_DIR}\" )")
128     ENDIF()
129   ENDFOREACH()
130
131   ADD_LIBRARY(${module} ${_type} ${_sources})
132   TARGET_LINK_LIBRARIES(${module} ${_linklibs})
133   SET_TARGET_PROPERTIES(${module} PROPERTIES COMPILE_FLAGS "${OMNIORB_DEFINITIONS}")
134
135 ENDMACRO(OMNIORB_ADD_MODULE)