Salome HOME
merge from BR_KERNEL_REFACTORING
[modules/kernel.git] / salome_adm / cmake_files / SalomeMacros.cmake
1 # Copyright (C) 2012  CEA/DEN, EDF R&D
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 #----------------------------------------------------------------------------
21 # LIST_CONTAINS is a macro useful for determining whether a list has a 
22 # particular entry
23 #----------------------------------------------------------------------------
24 MACRO(LIST_CONTAINS var value)
25   SET(${var})
26   FOREACH(value2 ${ARGN})
27     IF(${value} STREQUAL "${value2}")
28       SET(${var} TRUE)
29     ENDIF (${value} STREQUAL "${value2}")
30   ENDFOREACH (value2)
31 ENDMACRO(LIST_CONTAINS)
32
33 #----------------------------------------------------------------------------
34 # The PARSE_ARGUMENTS macro will take the arguments of another macro and
35 # define several variables.
36 #
37 # USAGE:  PARSE_ARGUMENTS(prefix arg_names options arg1 arg2...)
38 #
39 # ARGUMENTS:
40 #
41 # prefix: IN: a prefix to put on all variables it creates.
42 #
43 # arg_names: IN: a list of names.
44 # For each item in arg_names, PARSE_ARGUMENTS will create a 
45 # variable with that name, prefixed with prefix_. Each variable will be filled
46 # with the arguments that occur after the given arg_name is encountered
47 # up to the next arg_name or the end of the arguments. All options are
48 # removed from these lists. PARSE_ARGUMENTS also creates a
49 # prefix_DEFAULT_ARGS variable containing the list of all arguments up
50 # to the first arg_name encountered.
51 #
52 # options: IN: a list of options.
53 # For each item in options, PARSE_ARGUMENTS will create a
54 # variable with that name, prefixed with prefix_. So, for example, if prefix is
55 # MY_MACRO and options is OPTION1;OPTION2, then PARSE_ARGUMENTS will
56 # create the variables MY_MACRO_OPTION1 and MY_MACRO_OPTION2. These
57 # variables will be set to true if the option exists in the command line
58 # or false otherwise.
59 # arg_names and options lists should be quoted.
60 #
61 # The rest of PARSE_ARGUMENTS are arguments from another macro to be parsed.
62 #----------------------------------------------------------------------------
63 MACRO(PARSE_ARGUMENTS prefix arg_names option_names)
64   SET(DEFAULT_ARGS)
65   FOREACH(arg_name ${arg_names})
66     SET(${prefix}_${arg_name})
67   ENDFOREACH(arg_name)
68   FOREACH(option ${option_names})
69     SET(${prefix}_${option} FALSE)
70   ENDFOREACH(option)
71   SET(current_arg_name DEFAULT_ARGS)
72   SET(current_arg_list)
73   FOREACH(arg ${ARGN})
74     LIST_CONTAINS(is_arg_name ${arg} ${arg_names})
75     IF (is_arg_name)
76       SET(${prefix}_${current_arg_name} ${current_arg_list})
77       SET(current_arg_name ${arg})
78       SET(current_arg_list)
79     ELSE (is_arg_name)
80       LIST_CONTAINS(is_option ${arg} ${option_names})
81       IF (is_option)
82       SET(${prefix}_${arg} TRUE)
83       ELSE (is_option)
84       SET(current_arg_list ${current_arg_list} ${arg})
85       ENDIF (is_option)
86     ENDIF (is_arg_name)
87   ENDFOREACH(arg)
88   SET(${prefix}_${current_arg_name} ${current_arg_list})
89 ENDMACRO(PARSE_ARGUMENTS)
90
91 #----------------------------------------------------------------------------
92 # SALOME_INSTALL_SCRIPTS is a macro useful for installing scripts.
93 #
94 # USAGE: SALOME_INSTALL_SCRIPTS(file_list path [WORKING_DIRECTORY dir] [DEF_PERMS])
95 #
96 # ARGUMENTS:
97 # file_list: IN : list of files to be installed. This list should be quoted.
98 # path: IN : full pathname for installing.
99
100 # By default files to be installed as executable scripts.
101 # If DEF_PERMS option is provided, than permissions for installed files are
102 # only OWNER_WRITE, OWNER_READ, GROUP_READ, and WORLD_READ. 
103 #----------------------------------------------------------------------------
104 MACRO(SALOME_INSTALL_SCRIPTS file_list path)
105   PARSE_ARGUMENTS(SALOME_INSTALL_SCRIPTS "WORKING_DIRECTORY" "DEF_PERMS" ${ARGN})
106   SET(PERMS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
107   IF(NOT SALOME_INSTALL_SCRIPTS_DEF_PERMS)
108     SET(PERMS ${PERMS} OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE)
109   ENDIF(NOT SALOME_INSTALL_SCRIPTS_DEF_PERMS)
110   FOREACH(file ${file_list})
111     SET(PREFIX "")
112     IF(SALOME_INSTALL_SCRIPTS_WORKING_DIRECTORY)
113       SET(PREFIX "${SALOME_INSTALL_SCRIPTS_WORKING_DIRECTORY}/")
114     ENDIF(SALOME_INSTALL_SCRIPTS_WORKING_DIRECTORY)
115     INSTALL(FILES ${PREFIX}${file} DESTINATION ${path} PERMISSIONS ${PERMS})
116     GET_FILENAME_COMPONENT(ext ${file} EXT)
117     IF(ext STREQUAL .py)
118       INSTALL(CODE "MESSAGE(STATUS \"py compiling ${CMAKE_INSTALL_PREFIX}/${path}/${file}\")")
119       INSTALL(CODE "SET(CMD \"import py_compile ; py_compile.compile('${CMAKE_INSTALL_PREFIX}/${path}/${file}')\")")
120       INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c \"\${CMD}\")")
121       INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -O -c \"\${CMD}\")")
122     ENDIF(ext STREQUAL .py)
123   ENDFOREACH(file ${file_list})
124 ENDMACRO(SALOME_INSTALL_SCRIPTS)
125
126 #----------------------------------------------------------------------------
127 # SALOME_INSTALL_SCRIPTS is a macro useful for installing executable scripts.
128 # ARGUMENTS:
129 # PYFILE2COMPINST: IN : list of python files to be installed.
130 # PYFILELOC: IN : full pathname for installing.
131 # Permissions of installed files: OWNER_WRITE, OWNER_READ, GROUP_READ, and WORLD_READ
132 #----------------------------------------------------------------------------
133 MACRO(INSTALL_AND_COMPILE_PYTHON_FILE PYFILE2COMPINST PYFILELOC)
134   INSTALL(CODE "SET(PYTHON_FILE ${f})")
135   FOREACH(input ${PYFILE2COMPINST})
136     GET_FILENAME_COMPONENT(inputname ${input} NAME)
137     INSTALL(FILES ${input} DESTINATION ${CMAKE_INSTALL_PREFIX}/${PYFILELOC})
138     INSTALL(CODE "MESSAGE(STATUS \"py compiling ${CMAKE_INSTALL_PREFIX}/${PYFILELOC}/${inputname}\")")
139     INSTALL(CODE "SET(CMD \"import py_compile ; py_compile.compile('${CMAKE_INSTALL_PREFIX}/${PYFILELOC}/${inputname}')\")")
140     INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c \"\${CMD}\")")
141     INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -O -c \"\${CMD}\")")
142   ENDFOREACH(input ${PYFILE2COMPINST})
143 ENDMACRO(INSTALL_AND_COMPILE_PYTHON_FILE PYFILE2COMPINST PYFILELOC)
144
145 #----------------------------------------------------------------------------
146 # SALOME_CONFIGURE_FILE is a macro useful for copying a file to another location 
147 # and modify its contents.
148 #
149 # USAGE: SALOME_CONFIGURE_FILE(in_file out_file [INSTALL dir])
150 #
151 # ARGUMENTS:
152 # in_file: IN : input file with full paths.
153 # out_file: IN : output file with full paths.
154 # If INSTALL is specified, then 'out_file' will be installed to the 'dir' directory.
155 #----------------------------------------------------------------------------
156 MACRO(SALOME_CONFIGURE_FILE IN_FILE OUT_FILE)
157   MESSAGE(STATUS "Creation of ${OUT_FILE}")
158   CONFIGURE_FILE(${IN_FILE} ${OUT_FILE} @ONLY)
159   PARSE_ARGUMENTS(SALOME_CONFIGURE_FILE "INSTALL" "" ${ARGN})
160   IF(SALOME_CONFIGURE_FILE_INSTALL)
161     INSTALL(FILES ${OUT_FILE} DESTINATION ${SALOME_CONFIGURE_FILE_INSTALL})
162   ENDIF(SALOME_CONFIGURE_FILE_INSTALL)
163 ENDMACRO(SALOME_CONFIGURE_FILE)