1425bb815Sopenharmony_ci# Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci#
3425bb815Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci# you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci# You may obtain a copy of the License at
6425bb815Sopenharmony_ci#
7425bb815Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci#
9425bb815Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci# See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci# limitations under the License.
14425bb815Sopenharmony_ci
15425bb815Sopenharmony_cicmake_minimum_required (VERSION 2.8.12)
16425bb815Sopenharmony_ciset(JERRY_PORT_DEFAULT_NAME jerry-port-default)
17425bb815Sopenharmony_ciproject (${JERRY_PORT_DEFAULT_NAME} C)
18425bb815Sopenharmony_ci
19425bb815Sopenharmony_ci# Include directories
20425bb815Sopenharmony_ciset(INCLUDE_PORT_DEFAULT "${CMAKE_CURRENT_SOURCE_DIR}/include")
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_ci# Source directories
23425bb815Sopenharmony_cifile(GLOB SOURCE_PORT_DEFAULT *.c)
24425bb815Sopenharmony_ci
25425bb815Sopenharmony_ci# "Single" JerryScript source/header build.
26425bb815Sopenharmony_ci#  The process will create the following files:
27425bb815Sopenharmony_ci#   * jerryscript-port-default.c
28425bb815Sopenharmony_ci#   * jerryscript-port-default.h
29425bb815Sopenharmony_ciif(ENABLE_ALL_IN_ONE_SOURCE)
30425bb815Sopenharmony_ci  file(GLOB HEADER_PORT_DEFAULT *.h)
31425bb815Sopenharmony_ci  set(ALL_IN_FILE "${CMAKE_BINARY_DIR}/src/jerryscript-port-default.c")
32425bb815Sopenharmony_ci  set(ALL_IN_FILE_H "${CMAKE_BINARY_DIR}/src/jerryscript-port-default.h")
33425bb815Sopenharmony_ci
34425bb815Sopenharmony_ci  add_custom_command(OUTPUT ${ALL_IN_FILE} ${ALL_IN_FILE_H} ${JERRYSCRIPT_CONFIG_H}
35425bb815Sopenharmony_ci                     COMMAND python ${CMAKE_SOURCE_DIR}/tools/srcgenerator.py
36425bb815Sopenharmony_ci                             --jerry-port-default
37425bb815Sopenharmony_ci                             --output-dir ${CMAKE_BINARY_DIR}/src
38425bb815Sopenharmony_ci                     DEPENDS ${SOURCE_PORT_DEFAULT}
39425bb815Sopenharmony_ci                             ${HEADER_PORT_DEFAULT}
40425bb815Sopenharmony_ci                             ${CMAKE_SOURCE_DIR}/tools/srcgenerator.py
41425bb815Sopenharmony_ci                             ${CMAKE_SOURCE_DIR}/tools/srcmerger.py
42425bb815Sopenharmony_ci  )
43425bb815Sopenharmony_ci  add_custom_target(generate-single-source-port DEPENDS ${ALL_IN_FILE} ${ALL_IN_FILE_H})
44425bb815Sopenharmony_ci  add_dependencies(generate-single-source generate-single-source-port)
45425bb815Sopenharmony_ci
46425bb815Sopenharmony_ci  set(SOURCE_PORT_DEFAULT ${ALL_IN_FILE} ${ALL_IN_FILE_H})
47425bb815Sopenharmony_ciendif()
48425bb815Sopenharmony_ci
49425bb815Sopenharmony_ci# Define _BSD_SOURCE and _DEFAULT_SOURCE
50425bb815Sopenharmony_ci# (should only be necessary if we used compiler default libc but not checking that)
51425bb815Sopenharmony_ciset(DEFINES_PORT_DEFAULT _BSD_SOURCE _DEFAULT_SOURCE)
52425bb815Sopenharmony_ci
53425bb815Sopenharmony_ciINCLUDE (CheckStructHasMember)
54425bb815Sopenharmony_ci# CHECK_STRUCT_HAS_MEMBER works by trying to compile some C code that accesses the
55425bb815Sopenharmony_ci# given field of the given struct. However, our default compiler options break this
56425bb815Sopenharmony_ci# C code, so turn a couple of them off for this.
57425bb815Sopenharmony_ciset(CMAKE_REQUIRED_FLAGS "-Wno-error=strict-prototypes -Wno-error=old-style-definition")
58425bb815Sopenharmony_ci# tm.tm_gmtoff is non-standard, so glibc doesn't expose it in c99 mode
59425bb815Sopenharmony_ci# (our default). Define some macros to expose it anyway.
60425bb815Sopenharmony_ciset(CMAKE_REQUIRED_DEFINITIONS "-D_BSD_SOURCE -D_DEFAULT_SOURCE")
61425bb815Sopenharmony_ciCHECK_STRUCT_HAS_MEMBER ("struct tm" tm_gmtoff time.h HAVE_TM_GMTOFF)
62425bb815Sopenharmony_ciif(HAVE_TM_GMTOFF)
63425bb815Sopenharmony_ci  set(DEFINES_PORT_DEFAULT ${DEFINES_PORT_DEFAULT} HAVE_TM_GMTOFF)
64425bb815Sopenharmony_ciendif()
65425bb815Sopenharmony_ci
66425bb815Sopenharmony_ci# Sleep function availability check
67425bb815Sopenharmony_ciINCLUDE (CheckIncludeFiles)
68425bb815Sopenharmony_ciCHECK_INCLUDE_FILES (time.h HAVE_TIME_H)
69425bb815Sopenharmony_ciCHECK_INCLUDE_FILES (unistd.h HAVE_UNISTD_H)
70425bb815Sopenharmony_ciif(HAVE_TIME_H)
71425bb815Sopenharmony_ci  set(DEFINES_PORT_DEFAULT ${DEFINES_PORT_DEFAULT} HAVE_TIME_H)
72425bb815Sopenharmony_cielseif(HAVE_UNISTD_H)
73425bb815Sopenharmony_ci  set(DEFINES_PORT_DEFAULT ${DEFINES_PORT_DEFAULT} HAVE_UNISTD_H)
74425bb815Sopenharmony_ciendif()
75425bb815Sopenharmony_ci
76425bb815Sopenharmony_ci# Default Jerry port implementation library variants:
77425bb815Sopenharmony_ci#   - default
78425bb815Sopenharmony_ci#   - default-minimal (no extra termination and log APIs)
79425bb815Sopenharmony_ciforeach(JERRY_PORT_LIBRARY_NAME ${JERRY_PORT_DEFAULT_NAME} ${JERRY_PORT_DEFAULT_NAME}-minimal)
80425bb815Sopenharmony_ci  add_library(${JERRY_PORT_LIBRARY_NAME} ${SOURCE_PORT_DEFAULT})
81425bb815Sopenharmony_ci  target_include_directories(${JERRY_PORT_LIBRARY_NAME} PUBLIC ${INCLUDE_PORT_DEFAULT})
82425bb815Sopenharmony_ci  target_include_directories(${JERRY_PORT_LIBRARY_NAME} PRIVATE ${INCLUDE_CORE_PUBLIC})
83425bb815Sopenharmony_ci  target_include_directories(${JERRY_PORT_LIBRARY_NAME} PRIVATE ${INCLUDE_EXT_PUBLIC})
84425bb815Sopenharmony_ci  target_compile_definitions(${JERRY_PORT_LIBRARY_NAME} PRIVATE ${DEFINES_PORT_DEFAULT})
85425bb815Sopenharmony_ci  target_link_libraries(${JERRY_PORT_LIBRARY_NAME} jerry-core) # FIXME: remove this dependency as soon as possible
86425bb815Sopenharmony_ciendforeach()
87425bb815Sopenharmony_ci
88425bb815Sopenharmony_citarget_compile_definitions(${JERRY_PORT_DEFAULT_NAME}-minimal PRIVATE DISABLE_EXTRA_API)
89425bb815Sopenharmony_ci
90425bb815Sopenharmony_ci# Installation
91425bb815Sopenharmony_ciconfigure_file(libjerry-port-default.pc.in libjerry-port-default.pc @ONLY)
92425bb815Sopenharmony_ciconfigure_file(libjerry-port-default-minimal.pc.in libjerry-port-default-minimal.pc @ONLY)
93425bb815Sopenharmony_ci
94425bb815Sopenharmony_ciinstall(TARGETS ${JERRY_PORT_DEFAULT_NAME} ${JERRY_PORT_DEFAULT_NAME}-minimal DESTINATION lib)
95425bb815Sopenharmony_ciinstall(FILES ${CMAKE_CURRENT_BINARY_DIR}/libjerry-port-default.pc ${CMAKE_CURRENT_BINARY_DIR}/libjerry-port-default-minimal.pc DESTINATION lib/pkgconfig)
96425bb815Sopenharmony_ciinstall(DIRECTORY ${INCLUDE_PORT_DEFAULT}/ DESTINATION include)
97