• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright JS Foundation and other contributors, http://js.foundation
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
15cmake_minimum_required (VERSION 2.8.12)
16project (Jerry C)
17
18# Determining platform
19set(PLATFORM "${CMAKE_SYSTEM_NAME}")
20string(TOUPPER "${PLATFORM}" PLATFORM)
21
22# Determining compiler
23if(CMAKE_C_COMPILER_ID MATCHES "GNU")
24  set(USING_GCC 1)
25endif()
26
27if(CMAKE_C_COMPILER_ID MATCHES "Clang")
28  set(USING_CLANG 1)
29endif()
30
31if(CMAKE_C_COMPILER_ID MATCHES "TI")
32  set(USING_TI 1)
33endif()
34
35if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
36  set(USING_MSVC 1)
37endif()
38
39# Determining build type
40if(NOT CMAKE_BUILD_TYPE)
41  set(CMAKE_BUILD_TYPE "MinSizeRel")
42endif()
43
44# Optional components
45set(JERRY_CMDLINE           ON  CACHE BOOL "Build jerry command line tool?")
46set(JERRY_CMDLINE_TEST      OFF CACHE BOOL "Build jerry test command line tool?")
47set(JERRY_CMDLINE_SNAPSHOT  OFF CACHE BOOL "Build jerry snapshot command line tool?")
48set(JERRY_LIBFUZZER         OFF CACHE BOOL "Build jerry with libfuzzer support?")
49set(JERRY_PORT_DEFAULT      ON  CACHE BOOL "Build default jerry port implementation?")
50set(JERRY_EXT               ON  CACHE BOOL "Build jerry-ext?")
51set(JERRY_LIBM              ON  CACHE BOOL "Build and use jerry-libm?")
52set(UNITTESTS               OFF CACHE BOOL "Build unit tests?")
53set(DOCTESTS                OFF CACHE BOOL "Build doc tests?")
54
55# Optional build settings
56set(BUILD_SHARED_LIBS  OFF CACHE BOOL "Build shared libraries?")
57set(ENABLE_LTO         ON  CACHE BOOL "Enable LTO build?")
58set(ENABLE_STRIP       ON  CACHE BOOL "Enable stripping all symbols from release binary?")
59
60# Option overrides
61if(NOT USING_CLANG)
62  set(JERRY_LIBFUZZER OFF)
63
64  set(JERRY_LIBFUZZER_MESSAGE " (FORCED BY COMPILER)")
65endif()
66
67if(JERRY_CMDLINE OR JERRY_CMDLINE_TEST OR JERRY_CMDLINE_SNAPSHOT OR JERRY_LIBFUZZER OR UNITTESTS OR DOCTESTS)
68  set(JERRY_PORT_DEFAULT ON)
69
70  set(JERRY_PORT_DEFAULT_MESSAGE " (FORCED BY CMDLINE OR LIBFUZZER OR TESTS)")
71endif()
72
73if(JERRY_CMDLINE OR DOCTESTS)
74  set(JERRY_EXT ON)
75
76  set(JERRY_EXT_MESSAGE " (FORCED BY CMDLINE OR TESTS)")
77endif()
78
79if("${PLATFORM}" STREQUAL "DARWIN")
80  set(JERRY_LIBM         OFF)
81  set(ENABLE_LTO         OFF)
82  set(ENABLE_STRIP       OFF)
83
84  set(JERRY_LIBM_MESSAGE         " (FORCED BY PLATFORM)")
85  set(ENABLE_LTO_MESSAGE         " (FORCED BY PLATFORM)")
86  set(ENABLE_STRIP_MESSAGE       " (FORCED BY PLATFORM)")
87endif()
88
89if(USING_TI)
90  set(ENABLE_STRIP       OFF)
91
92  set(ENABLE_STRIP_MESSAGE       " (FORCED BY COMPILER)")
93endif()
94
95if(USING_MSVC)
96  set(JERRY_LIBM         OFF)
97  set(ENABLE_STRIP       OFF)
98
99  set(JERRY_LIBM_MESSAGE         " (FORCED BY COMPILER)")
100  set(ENABLE_STRIP_MESSAGE       " (FORCED BY COMPILER)")
101endif()
102
103if(CYGWIN)
104  set(ENABLE_LTO         OFF)
105
106  set(ENABLE_LTO_MESSAGE         " (FORCED BY PLATFORM)")
107endif()
108
109# Status messages
110message(STATUS "CMAKE_BUILD_TYPE            " ${CMAKE_BUILD_TYPE})
111message(STATUS "CMAKE_C_COMPILER_ID         " ${CMAKE_C_COMPILER_ID})
112message(STATUS "CMAKE_SYSTEM_NAME           " ${CMAKE_SYSTEM_NAME})
113message(STATUS "CMAKE_SYSTEM_PROCESSOR      " ${CMAKE_SYSTEM_PROCESSOR})
114message(STATUS "BUILD_SHARED_LIBS           " ${BUILD_SHARED_LIBS})
115message(STATUS "ENABLE_LTO                  " ${ENABLE_LTO} ${ENABLE_LTO_MESSAGE})
116message(STATUS "ENABLE_STRIP                " ${ENABLE_STRIP} ${ENABLE_STRIP_MESSAGE})
117message(STATUS "JERRY_CMDLINE               " ${JERRY_CMDLINE} ${JERRY_CMDLINE_MESSAGE})
118message(STATUS "JERRY_CMDLINE_TEST          " ${JERRY_CMDLINE_TEST} ${JERRY_CMDLINE_TEST_MESSAGE})
119message(STATUS "JERRY_CMDLINE_SNAPSHOT      " ${JERRY_CMDLINE_SNAPSHOT} ${JERRY_CMDLINE_SNAPSHOT_MESSAGE})
120message(STATUS "JERRY_LIBFUZZER             " ${JERRY_LIBFUZZER} ${JERRY_LIBFUZZER_MESSAGE})
121message(STATUS "JERRY_PORT_DEFAULT          " ${JERRY_PORT_DEFAULT} ${JERRY_PORT_DEFAULT_MESSAGE})
122message(STATUS "JERRY_EXT                   " ${JERRY_EXT} ${JERRY_EXT_MESSAGE})
123message(STATUS "JERRY_LIBM                  " ${JERRY_LIBM} ${JERRY_LIBM_MESSAGE})
124message(STATUS "UNITTESTS                   " ${UNITTESTS})
125message(STATUS "DOCTESTS                    " ${DOCTESTS})
126
127# Setup directories
128# Note: This mimics a conventional file system layout in the build directory for
129# the sake of convenient location of build artefacts. Proper installation to
130# traditional locations is also supported, e.g., to /usr/local.
131set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/")
132set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/")
133set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/")
134
135# Remove rdynamic option
136set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS )
137
138# Compile/link flags
139# Helper macros
140macro(jerry_add_flags VAR)
141  foreach(_flag ${ARGN})
142    set(${VAR} "${${VAR}} ${_flag}")
143  endforeach()
144endmacro()
145
146macro(jerry_add_compile_flags)
147  jerry_add_flags(CMAKE_C_FLAGS ${ARGV})
148endmacro()
149
150macro(jerry_add_compile_warnings)
151  foreach(_warning ${ARGV})
152    jerry_add_compile_flags(-W${_warning})
153    jerry_add_compile_flags(-Werror=${_warning})
154  endforeach()
155endmacro()
156
157macro(jerry_add_link_flags)
158  jerry_add_flags(LINKER_FLAGS_COMMON ${ARGV})
159endmacro()
160
161# Architecture-specific compile/link flags
162jerry_add_compile_flags(${FLAGS_COMMON_ARCH})
163jerry_add_flags(CMAKE_EXE_LINKER_FLAGS ${FLAGS_COMMON_ARCH})
164
165# LTO
166if(ENABLE_LTO)
167  if(USING_GCC OR USING_CLANG)
168    jerry_add_compile_flags(-flto)
169    jerry_add_link_flags(-flto)
170  endif()
171  if(USING_GCC)
172    jerry_add_compile_flags(-fno-fat-lto-objects)
173    # Use gcc-ar and gcc-ranlib to support LTO
174    set(CMAKE_AR "gcc-ar")
175    set(CMAKE_RANLIB "gcc-ranlib")
176  endif()
177  if(USING_TI)
178    jerry_add_link_flags(-lto)
179  endif()
180endif()
181
182# Compiler / Linker flags
183if("${PLATFORM}" STREQUAL "DARWIN")
184  jerry_add_link_flags(-lSystem)
185  set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Sqc <TARGET> <LINK_FLAGS> <OBJECTS>")
186  set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
187  set(CMAKE_SHARED_LINKER_FLAGS  "-undefined dynamic_lookup")
188elseif((NOT CYGWIN) AND (NOT CMAKE_SYSTEM_NAME STREQUAL "Windows") AND (USING_GCC OR USING_CLANG))
189  jerry_add_link_flags(-Wl,-z,noexecstack)
190endif()
191
192if(USING_GCC OR USING_CLANG)
193  jerry_add_compile_flags(-std=c99 -pedantic)
194  # Turn off stack protector
195  jerry_add_compile_flags(-fno-builtin -fno-stack-protector)
196  jerry_add_compile_warnings(all extra format-nonliteral init-self conversion sign-conversion format-security missing-declarations shadow strict-prototypes undef old-style-definition)
197  jerry_add_compile_flags(-Wno-stack-protector -Wno-attributes -Werror)
198endif()
199
200if(USING_GCC)
201  jerry_add_compile_warnings(logical-op)
202  # TODO: Remove workaround for gcc 7 bug if the fallthrough comment detection is fixed.
203  if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 7.0)
204    jerry_add_compile_flags(-Wno-implicit-fallthrough)
205  endif()
206endif()
207
208if(USING_CLANG)
209  jerry_add_compile_flags(-Wno-nested-anon-types -Wno-static-in-inline)
210endif()
211
212if(USING_TI)
213  jerry_add_compile_flags(--c99)
214endif()
215
216if(USING_MSVC)
217  jerry_add_link_flags(/OPT:NOREF)
218  # Disable MSVC warning 4996 globally because it stops us from using standard C functions.
219  jerry_add_compile_flags(/wd4996)
220endif()
221
222if(JERRY_LIBFUZZER)
223  jerry_add_compile_flags(-fsanitize=fuzzer-no-link)
224endif()
225
226# Strip binary
227if(ENABLE_STRIP AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
228  jerry_add_link_flags(-s)
229endif()
230
231# External compiler & linker flags
232if(DEFINED EXTERNAL_COMPILE_FLAGS)
233  jerry_add_compile_flags(${EXTERNAL_COMPILE_FLAGS})
234endif()
235
236if(DEFINED EXTERNAL_LINKER_FLAGS)
237  jerry_add_link_flags(${EXTERNAL_LINKER_FLAGS})
238endif()
239
240# Used for placeholder to attach single source build targets
241add_custom_target(generate-single-source)
242
243# Jerry's libm
244if(JERRY_LIBM)
245  add_subdirectory(jerry-libm)
246endif()
247
248# Jerry's core
249add_subdirectory(jerry-core)
250
251# Jerry's extension tools
252if(JERRY_EXT)
253  add_subdirectory(jerry-ext)
254endif()
255
256# Jerry's default port implementation
257if(JERRY_PORT_DEFAULT)
258  add_subdirectory(jerry-port/default)
259endif()
260
261# Jerry command line tool
262if(JERRY_CMDLINE OR JERRY_CMDLINE_TEST OR JERRY_CMDLINE_SNAPSHOT OR JERRY_LIBFUZZER)
263  add_subdirectory(jerry-main)
264endif()
265
266# Unittests
267if(UNITTESTS)
268  add_subdirectory(tests/unit-core)
269  if(JERRY_LIBM)
270    add_subdirectory(tests/unit-libm)
271  endif()
272  if(JERRY_EXT)
273    add_subdirectory(tests/unit-ext)
274  endif()
275endif()
276
277# Doctests
278if(DOCTESTS)
279  add_subdirectory(tests/unit-doc)
280endif()
281