Home | History | Annotate | Download | only in python
      1 # Copyright (c) 2015, Intel Corporation
      2 # All rights reserved.
      3 #
      4 # Redistribution and use in source and binary forms, with or without modification,
      5 # are permitted provided that the following conditions are met:
      6 #
      7 # 1. Redistributions of source code must retain the above copyright notice, this
      8 # list of conditions and the following disclaimer.
      9 #
     10 # 2. Redistributions in binary form must reproduce the above copyright notice,
     11 # this list of conditions and the following disclaimer in the documentation and/or
     12 # other materials provided with the distribution.
     13 #
     14 # 3. Neither the name of the copyright holder nor the names of its contributors
     15 # may be used to endorse or promote products derived from this software without
     16 # specific prior written permission.
     17 #
     18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
     22 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
     25 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 find_package(SWIG)
     30 # Hide these variables from CMake GUIs and `cmake -L`
     31 set_property(CACHE
     32     SWIG_DIR SWIG_EXECUTABLE SWIG_VERSION
     33     PROPERTY ADVANCED TRUE)
     34 if(NOT SWIG_FOUND)
     35     message(FATAL_ERROR
     36         "Swig is needed to compile the Python bindings but was not found."
     37         "Please either install swig2.0 or disable Python bindings by setting"
     38         "PYTHON_BINDINGS to OFF (e.g. '-DPYTHON_BINDINGS=OFF' in the"
     39         "invocation of CMake.). See the 'Compilation' section of README.md"
     40         "for more details.")
     41 endif()
     42 
     43 include(${SWIG_USE_FILE})
     44 
     45 # Force usage of Python 2.7.x ...
     46 find_package(PythonInterp 2.7 REQUIRED)
     47 
     48 # ... and force the libs to be at the same version as the interpreter
     49 find_package(PythonLibs ${PYTHON_VERSION_STRING} EXACT REQUIRED)
     50 include_directories(${PYTHON_INCLUDE_DIRS})
     51 
     52 set_property(SOURCE pfw.i PROPERTY CPLUSPLUS ON)
     53 set_property(SOURCE pfw.i PROPERTY SWIG_FLAGS "-Wall")
     54 if(FATAL_WARNINGS)
     55     set_property(SOURCE pfw.i APPEND PROPERTY SWIG_FLAGS "-Werror")
     56 endif()
     57 
     58 swig_add_module(PyPfw python pfw.i)
     59 swig_link_libraries(PyPfw parameter ${PYTHON_LIBRARIES})
     60 # For convenience, the global library output directory
     61 # (CMAKE_LIBRARY_OUTPUT_DIRECTORY) is set to ${PROJECT_BINARY_DIR}/lib, so that
     62 # all libs are generated in the same folder. This help writing test targets
     63 # (add_test) that do not need "make install" to be run.
     64 # However, putting _PyPfw.so in that folder defeats the purpose described
     65 # above because when running tests using the python bindings, the PYTHONPATH
     66 # needs to contain both _PyPfw.so and PyPfw.py. Without the line below,
     67 # _PyPfw.so would be put in ${PROJECT_BINARY_DIR}/lib while PyPfw.py is put in
     68 # ${CMAKE_CURRENT_BINARY_DIR}.
     69 set_property(TARGET _PyPfw PROPERTY LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
     70 
     71 # '-DSWIG_PYTHON_SILENT_MEMLEAK' is needed because the "memleak" warning
     72 # pollutes the standard output. At the time of writing, the only warning is
     73 # spurious anyway, as it relates to "ILogger *" which is an abstract
     74 # class/interface class and as such cannot be destroyed.
     75 # -Wno-error is set to prevent compilation failure in case of the code
     76 # generated by swig generates warnings. We don't apply the FATAL_WARNING policy
     77 # here, since we consider this generated code as external.
     78 target_compile_definitions(_PyPfw PRIVATE SWIG_PYTHON_SILENT_MEMLEAK)
     79 target_compile_options(_PyPfw PRIVATE -Wno-error)
     80 
     81 
     82 # Find the python modules install path.
     83 # plat_specific is needed because we are installing a shared-library python
     84 # module and not only a pure python module.
     85 # prefix='' makes get_python_lib return a relative path.
     86 execute_process(COMMAND
     87     ${PYTHON_EXECUTABLE} -c
     88         "from distutils import sysconfig;\\
     89          print(sysconfig.get_python_lib(plat_specific=True, prefix=''))"
     90     OUTPUT_VARIABLE PYTHON_MODULE_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
     91 
     92 install(TARGETS _PyPfw LIBRARY DESTINATION ${PYTHON_MODULE_PATH} COMPONENT python)
     93 
     94 # install the generated Python file as well
     95 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/PyPfw.py DESTINATION ${PYTHON_MODULE_PATH}
     96     COMPONENT python)
     97