Salome HOME
Build documentation under Windows.
[tools/configuration.git] / cmake / UseSphinx.cmake
1 ###########################################################################
2 # Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 include(SalomeMacros)
25
26 IF(NOT Sphinx_FOUND)
27    MESSAGE(FATAL_ERROR "Developer error -> UseSphinx file should be included after detection of the sphinx!")
28 ENDIF()
29
30 function(JOIN OUTPUT GLUE)
31     set(_TMP_RESULT "")
32     set(_GLUE "") # effective glue is empty at the beginning
33     foreach(arg ${ARGN})
34         set(_TMP_RESULT "${_TMP_RESULT}${_GLUE}${arg}")
35         set(_GLUE "${GLUE}")
36     endforeach()
37     set(${OUTPUT} "${_TMP_RESULT}" PARENT_SCOPE)
38 endfunction()
39
40 #----------------------------------------------------------------------------
41 # ADD_MULTI_LANG_DOCUMENTATION is a macro which adds sphinx multi-language 
42 # documentation.
43 #
44 # USAGE: ADD_MULTI_LANG_DOCUMENTATION(TARGET <target_name> MODULE <module_name>
45 #                                     LANGUAGES <languages_list>)
46 #
47 # ARGUMENTS:
48 # TARGET_NAME : IN : target name for the documentation
49 # MODULE : IN : SALOME module name
50 # LANGUAGES : IN : list of the languages
51 #----------------------------------------------------------------------------
52 MACRO(ADD_MULTI_LANG_DOCUMENTATION)
53   # Common options
54   SET(PAPEROPT_a4 "-D latex_paper_size=a4")
55
56   # Parse input argument
57   PARSE_ARGUMENTS(MULTI_LANG "TARGET_NAME;MODULE;LANGUAGES" "" ${ARGN})
58
59   # Content of the executable file to generate documentation
60   SET(CMDS)
61
62   JOIN(SPHINX_EXE " " ${SPHINX_EXECUTABLE})
63   STRING(REPLACE "$$" "$" SPHINX_EXE ${SPHINX_EXE})
64
65   IF(MULTI_LANG_LANGUAGES)
66     # 1. Options for generation POT files
67     SET(POT_SPHINXOPTS "-c ${CMAKE_CURRENT_BINARY_DIR} -b gettext ${CMAKE_CURRENT_SOURCE_DIR}/input potfiles")
68     SET(CMDS "${CMDS} ${SPHINX_EXE} ${POT_SPHINXOPTS}\n")
69
70     # 2. Update PO files options
71     SET(LANGS "")
72     FOREACH(lang ${MULTI_LANG_LANGUAGES})
73       SET(LANGS "${LANGS} -l ${lang}")
74     ENDFOREACH()
75     SET(PO_SPHINXOPTS "${PO_SPHINXOPTS} update -p potfiles ${LANGS}")
76     SET(CMDS "${CMDS} ${SPHINX_INTL_EXECUTABLE} ${PO_SPHINXOPTS}\n")
77
78     # 3. Build MO files
79     SET(CMDS "${CMDS} ${SPHINX_INTL_EXECUTABLE} build\n")
80   ENDIF()
81
82   # 4. Options for EN documentation
83   SET(SPHINXOPTS "-c ${CMAKE_CURRENT_BINARY_DIR} -d doctrees -b html ${PAPEROPT_a4} ${CMAKE_CURRENT_SOURCE_DIR}/input ${MULTI_LANG_MODULE}")
84   SET(CMDS "${CMDS} ${SPHINX_EXE} ${SPHINXOPTS}\n")
85
86   # 5. Options for other documentation
87   FOREACH(lang ${MULTI_LANG_LANGUAGES})
88     SET(${lang}_SPHINXOPTS "-c ${CMAKE_CURRENT_BINARY_DIR} -d doctrees -b html ${PAPEROPT_a4} -D language=${lang} ${CMAKE_CURRENT_SOURCE_DIR}/input ${MULTI_LANG_MODULE}_${lang}")
89     SET(CMDS "${CMDS} ${SPHINX_EXE} ${${lang}_SPHINXOPTS}\n")
90   ENDFOREACH()
91
92   # 6. Create command file
93   IF(WIN32)
94     SET(_ext "bat")
95     SET(_call_cmd "call")
96   ELSE()
97     SET(_ext "sh")
98     SET(_call_cmd ".")
99   ENDIF()
100   
101   SET(_env)
102   IF(WIN32)
103     SET(_env "IF ${_env}\"%SET_${MULTI_LANG_TARGET_NAME}_VARS%\"==\"1\" GOTO DO_GENERATION\n")
104   ENDIF()  
105   FOREACH(_item ${_${PROJECT_NAME}_EXTRA_ENV})
106     FOREACH(_val ${_${PROJECT_NAME}_EXTRA_ENV_${_item}})
107       IF(WIN32)
108         IF(${_item} STREQUAL "LD_LIBRARY_PATH")
109           SET(_item PATH)
110         ENDIF()
111         STRING(REPLACE "/" "\\" _env "${_env} @SET ${_item}=${_val}\;%${_item}%\n")        
112       ELSEIF(APPLE)
113         IF(${_item} STREQUAL "LD_LIBRARY_PATH")
114           SET(_env "${_env} export DYLD_LIBRARY_PATH=${_val}:\${DYLD_LIBRARY_PATH}\n")
115         ELSE()
116           SET(_env "${_env} export ${_item}=${_val}:\${${_item}}\n")
117         ENDIF()
118       ELSE()
119         SET(_env "${_env} export ${_item}=${_val}:\${${_item}}\n")
120       ENDIF()
121     ENDFOREACH()
122   ENDFOREACH()
123   IF(WIN32)
124     SET(_env "${_env}@SET SET_${MULTI_LANG_TARGET_NAME}_VARS=1\n")
125     SET(_env "${_env}:DO_GENERATION\n" )
126   ENDIF()
127
128   
129   SET(_script ${CMAKE_CURRENT_BINARY_DIR}/build_doc.${_ext})
130   FILE(WRITE ${_script} ${_env}${CMDS})
131
132   # 7. Create custom target
133   ADD_CUSTOM_TARGET(${MULTI_LANG_TARGET_NAME}
134                     # 1. Copy existing po files
135                     COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/locale ${CMAKE_CURRENT_BINARY_DIR}/locale
136                     # 2.  Generate documentation
137                     COMMAND ${_call_cmd} ${_script}
138                     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
139                    )
140
141   # 8. Update PO files
142   FOREACH(lang ${MULTI_LANG_LANGUAGES})
143     FILE(GLOB _pfiles ${CMAKE_CURRENT_BINARY_DIR}/locale/${lang}/LC_MESSAGES/*.po)
144     ADD_CUSTOM_COMMAND(TARGET ${MULTI_LANG_TARGET_NAME} POST_BUILD
145       COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_SOURCE_DIR}/locale/${lang}/LC_MESSAGES)
146     FOREACH(pofile ${_pfiles})
147       GET_FILENAME_COMPONENT(fn_wo_path ${pofile} NAME)
148       ADD_CUSTOM_COMMAND(TARGET ${MULTI_LANG_TARGET_NAME} POST_BUILD
149                          COMMAND ${CMAKE_COMMAND} -E
150                          copy_if_different ${pofile} ${CMAKE_CURRENT_SOURCE_DIR}/locale/${lang}/LC_MESSAGES/${fn_wo_path})
151     ENDFOREACH()
152   ENDFOREACH()
153
154   # 9. Make clean files/folders
155   SET(make_clean_files ${MULTI_LANG_MODULE} doctrees potfiles locale)
156   FOREACH(lang ${MULTI_LANG_LANGUAGES})
157     SET(make_clean_files ${make_clean_files} ${MULTI_LANG_MODULE}_${lang})
158   ENDFOREACH()
159   SET_DIRECTORY_PROPERTIES(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${make_clean_files}")
160
161 ENDMACRO(ADD_MULTI_LANG_DOCUMENTATION)