1 # Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 cmake_minimum_required(VERSION 2.8)
16 include(CheckCCompilerFlag)
20 set(IOTJS_VERSION_MAJOR 1)
21 set(IOTJS_VERSION_MINOR 0)
23 if(NOT DEFINED TARGET_OS)
24 string(TOLOWER ${CMAKE_SYSTEM_NAME} TARGET_OS)
26 "TARGET_OS not specified, using '${TARGET_OS}' from CMAKE_SYSTEM_NAME")
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)
37 if(NOT DEFINED BUILD_LIB_ONLY)
38 set(BUILD_LIB_ONLY OFF)
41 if(NOT DEFINED ENABLE_SNAPSHOT)
42 message("Snapshot mode force enabled")
43 set(ENABLE_SNAPSHOT ON)
46 if(NOT DEFINED ENABLE_LTO)
47 message("LTO force disabled")
51 macro(iotjs_add_flags VAR)
52 foreach(_flag ${ARGN})
53 set(${VAR} "${${VAR}} ${_flag}")
57 macro(iotjs_add_compile_flags)
58 iotjs_add_flags(CMAKE_C_FLAGS ${ARGV})
61 macro(iotjs_add_link_flags)
62 iotjs_add_flags(IOTJS_LINKER_FLAGS ${ARGV})
65 macro(build_lib_name LIB_VAR NAME)
67 ${CMAKE_STATIC_LIBRARY_PREFIX}${NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
70 if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
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)
77 CHECK_C_COMPILER_FLAG(-no-pie HAS_NO_PIE)
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)
86 iotjs_add_compile_flags(-fPIE)
87 if("${TARGET_OS}" STREQUAL "darwin")
88 iotjs_add_link_flags(-Wl,-pie)
90 iotjs_add_link_flags(-pie)
94 if (CREATE_SHARED_LIB)
95 iotjs_add_compile_flags(-fPIC)
99 iotjs_add_compile_flags(-DEXPERIMENTAL)
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__)
108 iotjs_add_compile_flags(-march=i686 -m32)
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!")
115 message(FATAL_ERROR "Missing --experimental build option for MIPS!")
119 message(FATAL_ERROR "Cross endian snapshots are not supported. "
120 "Please disable snapshot mode for mips!")
122 elseif("${TARGET_ARCH}" STREQUAL "noarch")
124 message(WARNING "Unknown target arch: ${TARGET_ARCH}.")
127 # Add board-dependant flags
128 iotjs_add_compile_flags(-DTARGET_BOARD=${TARGET_BOARD})
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)
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)
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!")
165 message(FATAL_ERROR "Missing --experimental build option for Windows!")
167 elseif("${TARGET_OS}" STREQUAL "openwrt")
168 message("OpenWrt support is experimental!")
170 message(FATAL_ERROR "Missing --experimental build option for OpenWrt!")
173 iotjs_add_compile_flags(-D__OPENWRT__ -D_GNU_SOURCE)
174 iotjs_add_link_flags(-pthread)
176 message(WARNING "Unknown target os: ${TARGET_OS}.")
179 # Add external options
180 if(DEFINED EXTERNAL_COMPILE_FLAGS)
181 iotjs_add_compile_flags(${EXTERNAL_COMPILE_FLAGS})
184 if(DEFINED EXTERNAL_LINKER_FLAGS)
185 iotjs_add_link_flags(${EXTERNAL_LINKER_FLAGS})
188 string(TOUPPER "${TARGET_OS}" TARGET_OS)
190 set(ROOT_DIR ${CMAKE_SOURCE_DIR})
191 set(ARCHIVE_DIR ${CMAKE_BINARY_DIR}/lib)
193 include(ExternalProject)
195 if(NOT ${EXTERNAL_LIBC_INTERFACE} STREQUAL "")
196 iotjs_add_compile_flags(-isystem ${EXTERNAL_LIBC_INTERFACE})
199 # Include external projects
200 include(cmake/jerry.cmake)
201 include(cmake/http-parser.cmake)
202 include(cmake/libtuv.cmake)
204 include(cmake/iotjs.cmake)