support armv7hl build
[platform/upstream/iotjs.git] / CMakeLists.txt
1 # Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 cmake_minimum_required(VERSION 2.8)
16 include(CheckCCompilerFlag)
17
18 project(IOTJS C)
19
20 set(IOTJS_VERSION_MAJOR 1)
21 set(IOTJS_VERSION_MINOR 0)
22
23 if(NOT DEFINED TARGET_OS)
24   string(TOLOWER ${CMAKE_SYSTEM_NAME} TARGET_OS)
25   message(
26     "TARGET_OS not specified, using '${TARGET_OS}' from CMAKE_SYSTEM_NAME")
27 endif()
28
29 if(NOT CMAKE_BUILD_TYPE)
30   message("CMAKE_BUILD_TYPE was not set! Configuring for Debug build!")
31   set(CMAKE_BUILD_TYPE Debug)
32 elseif("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
33   message("CMAKE_BUILD_TYPE was set to Release, switching to MinSizeRel")
34   set(CMAKE_BUILD_TYPE MinSizeRel)
35 endif()
36
37 if(NOT DEFINED BUILD_LIB_ONLY)
38   set(BUILD_LIB_ONLY OFF)
39 endif()
40
41 if(NOT DEFINED ENABLE_SNAPSHOT)
42   message("Snapshot mode force enabled")
43   set(ENABLE_SNAPSHOT ON)
44 endif()
45
46 if(NOT DEFINED ENABLE_LTO)
47   message("LTO force disabled")
48   set(ENABLE_LTO OFF)
49 endif()
50
51 macro(iotjs_add_flags VAR)
52   foreach(_flag ${ARGN})
53     set(${VAR} "${${VAR}} ${_flag}")
54   endforeach()
55 endmacro()
56
57 macro(iotjs_add_compile_flags)
58   iotjs_add_flags(CMAKE_C_FLAGS ${ARGV})
59 endmacro()
60
61 macro(iotjs_add_link_flags)
62   iotjs_add_flags(IOTJS_LINKER_FLAGS ${ARGV})
63 endmacro()
64
65 macro(build_lib_name LIB_VAR NAME)
66   set(${LIB_VAR}
67       ${CMAKE_STATIC_LIBRARY_PREFIX}${NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
68 endmacro()
69
70 if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
71   set(USING_MSVC 1)
72   set(CONFIG_TYPE $<$<CONFIG:Debug>:Debug>$<$<CONFIG:Release>:Release>)
73   # disable warning C4820: 'x' bytes padding added after construct 'membername'
74   iotjs_add_compile_flags(-wd4820)
75 endif()
76
77 CHECK_C_COMPILER_FLAG(-no-pie HAS_NO_PIE)
78
79 # Add buildtype-related flags
80 if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
81   iotjs_add_compile_flags(-DDEBUG -DENABLE_DEBUG_LOG)
82   if(HAS_NO_PIE AND NOT "${TARGET_OS}" STREQUAL "darwin")
83     iotjs_add_link_flags(-no-pie)
84   endif()
85 else()
86   iotjs_add_compile_flags(-fPIE)
87   if("${TARGET_OS}" STREQUAL "darwin")
88     iotjs_add_link_flags(-Wl,-pie)
89   else()
90     iotjs_add_link_flags(-pie)
91   endif()
92 endif()
93
94 if (CREATE_SHARED_LIB)
95   iotjs_add_compile_flags(-fPIC)
96 endif()
97
98 if(EXPERIMENTAL)
99   iotjs_add_compile_flags(-DEXPERIMENTAL)
100 endif()
101
102 # Add arch-dependant flags
103 if("${TARGET_ARCH}" STREQUAL "arm")
104   iotjs_add_compile_flags(-D__arm__ -mthumb -fno-short-enums -mlittle-endian)
105 elseif("${TARGET_ARCH}" STREQUAL "i686")
106   iotjs_add_compile_flags(-D__i686__ -D__x86__)
107   if(NOT USING_MSVC)
108     iotjs_add_compile_flags(-march=i686 -m32)
109   endif()
110 elseif("${TARGET_ARCH}" STREQUAL "x86_64")
111   iotjs_add_compile_flags(-D__x86_64__)
112 elseif("${TARGET_ARCH}" STREQUAL "mips")
113   message("MIPS support is experimental!")
114   if(NOT EXPERIMENTAL)
115     message(FATAL_ERROR "Missing --experimental build option for MIPS!")
116   endif()
117
118   if(ENABLE_SNAPSHOT)
119     message(FATAL_ERROR "Cross endian snapshots are not supported. "
120                         "Please disable snapshot mode for mips!")
121   endif()
122 elseif("${TARGET_ARCH}" STREQUAL "noarch")
123 else()
124   message(WARNING "Unknown target arch: ${TARGET_ARCH}.")
125 endif()
126
127 # Add board-dependant flags
128 iotjs_add_compile_flags(-DTARGET_BOARD=${TARGET_BOARD})
129
130 if("${TARGET_BOARD}" STREQUAL "artik05x")
131   iotjs_add_compile_flags(-mcpu=cortex-r4 -mfpu=vfp3)
132 elseif("${TARGET_BOARD}" STREQUAL "artik10")
133   iotjs_add_compile_flags(-mcpu=cortex-a7 -mfpu=neon-vfpv4 -mfloat-abi=softfp)
134 elseif("${TARGET_BOARD}" STREQUAL "rpi2")
135   iotjs_add_compile_flags(-mcpu=cortex-a7 -mfpu=neon-vfpv4)
136 elseif("${TARGET_BOARD}" STREQUAL "rpi3")
137   if("${TARGET_OS}" STREQUAL "tizen")
138     iotjs_add_compile_flags(-mfpu=neon-vfpv4)
139     endif()
140 elseif("${TARGET_BOARD}" STREQUAL "stm32f4dis")
141   iotjs_add_compile_flags(-mcpu=cortex-m4 -march=armv7e-m -mfpu=fpv4-sp-d16)
142   iotjs_add_compile_flags(-mfloat-abi=hard)
143 endif()
144
145 # Add os-dependant flags
146 if("${TARGET_OS}" STREQUAL "darwin")
147   iotjs_add_compile_flags(-D__DARWIN__ -fno-builtin)
148 elseif("${TARGET_OS}" STREQUAL "linux")
149   iotjs_add_compile_flags(-D__LINUX__ -fno-builtin)
150   iotjs_add_link_flags(-pthread -rdynamic)
151   iotjs_add_flags(EXTERNAL_LIBS m rt)
152 elseif("${TARGET_OS}" STREQUAL "nuttx")
153   iotjs_add_compile_flags(-D__NUTTX__ -Os -fno-strict-aliasing)
154   iotjs_add_compile_flags(-fno-strength-reduce -fomit-frame-pointer)
155 elseif("${TARGET_OS}" STREQUAL "tizen")
156   iotjs_add_compile_flags(-D__TIZEN__ -fno-builtin)
157   iotjs_add_link_flags(-pthread -rdynamic)
158   iotjs_add_flags(EXTERNAL_LIBS m rt)
159 elseif("${TARGET_OS}" STREQUAL "tizenrt")
160   iotjs_add_compile_flags(-D__TIZENRT__ -Os -fno-strict-aliasing)
161   iotjs_add_compile_flags(-fno-strength-reduce -fomit-frame-pointer)
162 elseif("${TARGET_OS}" STREQUAL "windows")
163   message("Windows support is experimental!")
164   if(NOT EXPERIMENTAL)
165     message(FATAL_ERROR "Missing --experimental build option for Windows!")
166   endif()
167 elseif("${TARGET_OS}" STREQUAL "openwrt")
168   message("OpenWrt support is experimental!")
169   if(NOT EXPERIMENTAL)
170     message(FATAL_ERROR "Missing --experimental build option for OpenWrt!")
171   endif()
172
173   iotjs_add_compile_flags(-D__OPENWRT__ -D_GNU_SOURCE)
174   iotjs_add_link_flags(-pthread)
175 else()
176   message(WARNING "Unknown target os: ${TARGET_OS}.")
177 endif()
178
179 # Add external options
180 if(DEFINED EXTERNAL_COMPILE_FLAGS)
181   iotjs_add_compile_flags(${EXTERNAL_COMPILE_FLAGS})
182 endif()
183
184 if(DEFINED EXTERNAL_LINKER_FLAGS)
185   iotjs_add_link_flags(${EXTERNAL_LINKER_FLAGS})
186 endif()
187
188 string(TOUPPER "${TARGET_OS}" TARGET_OS)
189
190 set(ROOT_DIR ${CMAKE_SOURCE_DIR})
191 set(ARCHIVE_DIR ${CMAKE_BINARY_DIR}/lib)
192
193 include(ExternalProject)
194
195 if(NOT ${EXTERNAL_LIBC_INTERFACE} STREQUAL "")
196   iotjs_add_compile_flags(-isystem ${EXTERNAL_LIBC_INTERFACE})
197 endif()
198
199 # Include external projects
200 include(cmake/jerry.cmake)
201 include(cmake/http-parser.cmake)
202 include(cmake/libtuv.cmake)
203
204 include(cmake/iotjs.cmake)