1cmake_minimum_required (VERSION 2.8)
2project (mraa C)
3
4FIND_PACKAGE (Threads REQUIRED)
5
6set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -fno-omit-frame-pointer ")
7set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall ")
8set (LIB_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}" CACHE PATH "Installation path for libraries")
9
10# Set CMAKE_LIB_INSTALL_DIR if not defined
11include(GNUInstallDirs)
12
13# By default, build shared object libraries on linux
14if (UNIX AND NOT APPLE)
15  if (NOT DEFINED BUILD_SHARED_LIBS)
16    set(BUILD_SHARED_LIBS ON)
17  endif()
18endif()
19
20# Appends the cmake/modules path to MAKE_MODULE_PATH variable.
21set (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
22
23# Make a version file containing the current version from git.
24include (GetGitRevisionDescription)
25git_describe (VERSION "--tags")
26if ("x_${VERSION}" STREQUAL "x_GIT-NOTFOUND" OR "x_${VERSION}" STREQUAL "x_HEAD-HASH-NOTFOUND" OR "x_${VERSION}" STREQUAL "x_-128-NOTFOUND")
27  message (WARNING " - Install git to compile a production libmraa!")
28  set (VERSION "v0.9.0-dirty")
29endif ()
30
31message (INFO " - libmraa Version ${VERSION}")
32
33#parse the version information into pieces.
34string (REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${VERSION}")
35string (REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${VERSION}")
36string (REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${VERSION}")
37string (REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+\\-([0-9]+).*" "\\1" VERSION_COMMIT "${VERSION}")
38string (REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+-[0-9]+\\-(.*)" "\\1" VERSION_SHA1 "${VERSION}")
39set (VERSION_SHORT "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
40
41if ("${VERSION_COMMIT}" MATCHES "^v.*")
42  set (VERSION_COMMIT "")
43endif()
44
45configure_file (${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/version.c.in
46                ${CMAKE_CURRENT_BINARY_DIR}/src/version.c)
47
48# this is the library version, independant of git revision
49set (mraa_VERSION_MAJOR ${VERSION_MAJOR})
50set (mraa_VERSION_MINOR ${VERSION_MINOR})
51set (mraa_VERSION_PATCH ${VERSION_PATCH})
52set (mraa_VERSION_STRING ${mraa_VERSION_MAJOR}.${mraa_VERSION_MINOR}.${mraa_VERSION_PATCH})
53
54set (CMAKE_SWIG_FLAGS "")
55
56find_path (SYSTEM_USR_DIR "stdlib.h")
57include_directories (${SYSTEM_USR_DIR})
58
59option (BUILDDOC "Build all doc." OFF)
60option (BUILDSWIG "Build swig modules." ON)
61option (BUILDSWIGPYTHON "Build swig python modules." ON)
62option (BUILDSWIGNODE "Build swig node modules." ON)
63option (BUILDSWIGJAVA "Build Java API." OFF)
64option (USBPLAT "Detection USB platform." OFF)
65option (FTDI4222 "Build with FTDI FT4222 subplatform support." OFF)
66option (IPK "Generate IPK using CPack" OFF)
67option (RPM "Generate RPM using CPack" OFF)
68option (BUILDPYTHON3 "Use python3 for building/installing/testing" OFF)
69option (ENABLEEXAMPLES "Disable building of examples" ON)
70option (INSTALLGPIOTOOL "Install gpio tool" OFF)
71option (BUILDARCH "Override architecture to build for - override" OFF)
72option (BUILDTESTS "Override the addition of tests" ON)
73
74set (MRAAPLATFORMFORCE "" CACHE STRING "ALL")
75
76if (NOT BUILDSWIG)
77  set (BUILDSWIGPYTHON OFF)
78  set (BUILDSWIGNODE OFF)
79  set (BUILDSWIGJAVA OFF)
80endif()
81
82if (NOT BUILDARCH)
83  include (TargetArch)
84  target_architecture (DETECTED_ARCH)
85  message( INFO " - Target arch is ${DETECTED_ARCH}")
86else ()
87  set (DETECTED_ARCH ${BUILDARCH})
88  message( INFO " - Override arch is ${DETECTED_ARCH}")
89endif()
90
91if (DETECTED_ARCH STREQUAL "i586" OR DETECTED_ARCH STREQUAL "x86_64"
92    OR DETECTED_ARCH STREQUAL "i386")
93  set (X86PLAT ON)
94elseif (DETECTED_ARCH MATCHES "arm.*")
95  set (ARMPLAT ON)
96else ()
97  message(FATAL_ERROR "Only x86 and arm platforms currently supported")
98endif()
99
100if (BUILDSWIGPYTHON OR BUILDTESTS)
101  if (BUILDPYTHON3)
102    set (PYTHONBUILD_VERSION 3)
103  else ()
104    set (PYTHONBUILD_VERSION 2.7)
105  endif ()
106  find_package (PythonInterp ${PYTHONBUILD_VERSION} REQUIRED)
107endif ()
108
109if (BUILDDOC)
110  # add a target to generate API documentation with Doxygen
111  find_package (Doxygen)
112  if (DOXYGEN_FOUND)
113    configure_file (${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
114    if (BUILDSWIGJAVA)
115      configure_file (${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.java.in ${CMAKE_CURRENT_BINARY_DIR}/src/java/Doxyfile @ONLY)
116    endif ()
117    add_custom_target (doc
118      ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
119      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
120      COMMENT "Generating API documentation with Doxygen" VERBATIM
121    )
122  endif (DOXYGEN_FOUND)
123endif ()
124
125if (IPK)
126  # Get target package arch from Yocto ADT sysroot if set or host OS, mapping to Ubuntu name if necessary
127  if (DEFINED ENV{OECORE_TARGET_SYSROOT})
128    GET_FILENAME_COMPONENT (DETECTED_SYSROOT $ENV{OECORE_TARGET_SYSROOT} NAME)
129    string (REGEX REPLACE "-poky-linux" "" TARGET_ARCH "${DETECTED_SYSROOT}")
130  else ()
131    # debian uses amd64 to denote x86_64
132    if (DETECTED_ARCH STREQUAL "x86_64")
133      set (TARGET_ARCH "amd64")
134    else ()
135      set (TARGET_ARCH ${DETECTED_ARCH})
136    endif ()
137  endif ()
138  message (INFO " - Package arch is ${TARGET_ARCH}")
139
140  set(CPACK_GENERATOR "DEB" "TGZ")
141  set(OPKG_ARCH ${TARGET_ARCH})
142  set(CPACK_BINARY_DIR ${CMAKE_BINARY_DIR})
143  set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Intel IoT-Devkit") #required
144  set(mraa_PACKAGE_ON_TAG ".")
145  if ("${VERSION_COMMIT}" STREQUAL "")
146    set(mraa_PACKAGE_ON_TAG "")
147  endif()
148  set(CPACK_PACKAGE_VERSION
149      "${mraa_VERSION_MAJOR}.${mraa_VERSION_MINOR}.${mraa_VERSION_PATCH}${mraa_PACKAGE_ON_TAG}${VERSION_COMMIT}")
150  set(CPACK_PACKAGE_NAME "mraa")
151  set(CPACK_DEBIAN_PACKAGE_SECTION "libs")
152  set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${TARGET_ARCH})
153  set(CPACK_SYSTEM_NAME ${TARGET_ARCH})
154  set(CPACK_DEBIAN_PACKAGE_PROVIDES "mraa-dev, mraa-dbg, mraa-doc")
155  set(CPACK_DEBIAN_PACKAGE_REPLACES "${CPACK_DEBIAN_PACKAGE_PROVIDES}, libmraa, libmraa-dev, libmraa-doc")
156  set(CPACK_DEBIAN_PACKAGE_CONFLICTS ${CPACK_DEBIAN_PACKAGE_PROVIDES})
157  set(WDIR "${CPACK_TOPLEVEL_DIRECTORY}/${CPACK_PACKAGE_FILE_NAME}")
158  include (CPack)
159endif()
160
161if (RPM)
162  message (INFO " - Enabled RPM packaging for ${DETECTED_ARCH}")
163  set(CMAKE_INSTALL_PREFIX "/usr")
164  set(CPACK_PACKAGE_VERSION ${VERSION})
165  set(CPACK_GENERATOR "RPM")
166  set(CPACK_PACKAGE_NAME "libmraa${mraa_VERSION_MAJOR}")
167  set(CPACK_PACKAGE_RELEASE 1)
168  set(CPACK_PACKAGE_VERSION
169      "${mraa_VERSION_MAJOR}.${mraa_VERSION_MINOR}.${mraa_VERSION_PATCH}${mraa_PACKAGE_ON_TAG}${VERSION_COMMIT}")
170  set(CPACK_PACKAGE_CONTACT "Intel IoT-Devkit")
171  set(CPACK_PACKAGE_VENDOR "Intel IoT-Devkit")
172  set(CPACK_RPM_PACKAGE_PROVIDES "${CPACK_PACKAGE_NAME}-devel")
173  # Get distro tag (e.g. 'fc20') by parsing output of rpm --showrc
174  EXECUTE_PROCESS(
175    COMMAND rpm --showrc
176    COMMAND grep -w dist
177    COMMAND sed -e "s/\\t./ /"
178    COMMAND awk "{printf \"%s\", \$NF}"
179    OUTPUT_VARIABLE DIST_TAG
180  )
181  set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_PACKAGE_RELEASE}.${DIST_TAG}.${DETECTED_ARCH}")
182  include(CPack)
183endif()
184
185add_subdirectory (src)
186if (ENABLEEXAMPLES)
187  add_subdirectory (examples)
188endif ()
189
190if (BUILDTESTS)
191  if (${PYTHONINTERP_FOUND})
192    enable_testing ()
193    add_subdirectory (tests)
194  endif ()
195endif ()
196