xref: /aosp_15_r20/external/sandboxed-api/contrib/jsonnet/CMakeLists.txt (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1# Copyright 2020 Google LLC
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#     https://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 3.13..3.26)
16
17project(jsonnet-sapi C CXX)
18include(CTest)
19include(GoogleTest)
20
21set(CMAKE_CXX_STANDARD 17)
22set(CMAKE_CXX_STANDARD_REQUIRED True)
23
24if(NOT TARGET sapi::sapi)
25  set(SAPI_ROOT "../.." CACHE PATH "Path to the Sandboxed API source tree")
26  add_subdirectory("${SAPI_ROOT}"
27                   "${CMAKE_BINARY_DIR}/sandboxed-api-build"
28                   EXCLUDE_FROM_ALL)
29endif()
30
31FetchContent_Declare(jsonnet
32  GIT_REPOSITORY https://github.com/google/jsonnet.git
33  GIT_TAG        v0.18.0 # 2021-12-21
34)
35set(BUILD_TESTS OFF CACHE BOOL "" FORCE) # Do not build jsonnet tests
36FetchContent_MakeAvailable(jsonnet)
37create_directory_symlink("${jsonnet_SOURCE_DIR}"
38                         "${PROJECT_BINARY_DIR}/jsonnet")
39
40configure_file("${jsonnet_SOURCE_DIR}/cmd/jsonnet.cpp"
41               "${PROJECT_BINARY_DIR}/gen_files/jsonnet.cpp" COPYONLY)
42
43add_custom_command(
44  OUTPUT "${PROJECT_BINARY_DIR}/gen_files/write_helper.cc"
45  WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/gen_files"
46  COMMAND patch -o write_helper.cc
47            < "${PROJECT_SOURCE_DIR}/jsonnet.patch" > /dev/null
48)
49
50add_library(jsonnet_helper STATIC
51  "${PROJECT_BINARY_DIR}/gen_files/write_helper.cc"
52  "${jsonnet_SOURCE_DIR}/cmd/utils.cpp"
53  "${jsonnet_SOURCE_DIR}/cmd/utils.h"
54  jsonnet_helper.cc
55  jsonnet_helper.h
56)
57add_library(sapi_contrib::jsonnet_helper ALIAS jsonnet_helper)
58target_include_directories(jsonnet_helper PUBLIC
59  "${PROJECT_BINARY_DIR}"
60  "${PROJECT_BINARY_DIR}/gen_files"
61  "${SAPI_SOURCE_DIR}"
62)
63target_link_libraries(jsonnet_helper
64  libjsonnet_for_binaries
65)
66
67add_sapi_library(jsonnet_sapi
68  FUNCTIONS c_free_input
69            c_jsonnet_destroy
70            c_jsonnet_evaluate_snippet
71            c_jsonnet_evaluate_snippet_multi
72            c_jsonnet_evaluate_snippet_stream
73            c_jsonnet_fmt_snippet
74            c_jsonnet_make
75            c_jsonnet_realloc
76            c_read_input
77            c_write_multi_output_files
78            c_write_output_file
79            c_write_output_stream
80  INPUTS jsonnet_helper.h
81  LIBRARY jsonnet_helper
82  LIBRARY_NAME Jsonnet
83  NAMESPACE ""
84)
85add_library(sapi_contrib::jsonnet ALIAS jsonnet_sapi)
86target_include_directories(jsonnet_sapi INTERFACE
87  "${PROJECT_BINARY_DIR}"
88)
89target_link_libraries(jsonnet_sapi PUBLIC
90  sapi_contrib::jsonnet_helper
91)
92
93if(SAPI_BUILD_EXAMPLES)
94  add_subdirectory(examples)
95endif()
96
97if(BUILD_TESTING AND SAPI_BUILD_TESTING)
98  # Create directories so the tests will be able to access them
99  file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/tests_input"
100                      "${PROJECT_BINARY_DIR}/tests_output"
101                      "${PROJECT_BINARY_DIR}/tests_expected_output")
102
103  add_custom_target(test_preparation ALL
104    COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes/*
105              ${PROJECT_BINARY_DIR}/tests_input
106    COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/*
107              ${PROJECT_BINARY_DIR}/tests_expected_output
108  )
109
110  add_executable(jsonnet_tests
111    jsonnet_tests.cc
112  )
113  target_include_directories(jsonnet_tests PUBLIC
114    "${PROJECT_SOURCE_DIR}"
115  )
116  target_link_libraries(jsonnet_tests
117    sapi_contrib::jsonnet
118    sapi::test_main
119  )
120  gtest_discover_tests(jsonnet_tests)
121endif()
122