1# Copyright 2020 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# 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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15import("//build_overrides/pigweed.gni")
16
17import("$dir_pw_arduino_build/arduino.gni")
18import("$dir_pw_build/facade.gni")
19import("$dir_pw_build/module_config.gni")
20import("$dir_pw_build/target_types.gni")
21import("$dir_pw_docgen/docs.gni")
22import("$dir_pw_fuzzer/fuzzer.gni")
23import("$dir_pw_unit_test/test.gni")
24import("backend.gni")
25
26declare_args() {
27  # The build target that overrides the default configuration options for this
28  # module. This should point to a source set that provides defines through a
29  # public config (which may -include a file or add defines directly).
30  pw_tokenizer_CONFIG = pw_build_DEFAULT_MODULE_CONFIG
31}
32
33config("public_include_path") {
34  include_dirs = [ "public" ]
35  visibility = [ ":*" ]
36}
37
38config("linker_script") {
39  inputs = [ "pw_tokenizer_linker_sections.ld" ]
40
41  # Automatically add the tokenizer linker sections when cross-compiling or
42  # building for Linux. macOS and Windows executables are not supported.
43  if (current_os == "") {
44    ldflags = [
45      "-T",
46      rebase_path("pw_tokenizer_linker_sections.ld"),
47    ]
48  } else if (current_os == "linux" && !pw_toolchain_OSS_FUZZ_ENABLED) {
49    # When building for Linux, the linker provides a default linker script.
50    # The add_tokenizer_sections_to_default_script.ld wrapper includes the
51    # pw_tokenizer_linker_sections.ld script in a way that appends to the the
52    # default linker script instead of overriding it.
53    ldflags = [
54      "-T",
55      rebase_path("add_tokenizer_sections_to_default_script.ld"),
56      "-L",
57      rebase_path("."),
58    ]
59    inputs += [ "add_tokenizer_sections_to_default_script.ld" ]
60  }
61  visibility = [ ":*" ]
62}
63
64pw_source_set("config") {
65  public = [ "public/pw_tokenizer/config.h" ]
66  public_configs = [ ":public_include_path" ]
67  public_deps = [ pw_tokenizer_CONFIG ]
68}
69
70pw_source_set("pw_tokenizer") {
71  public_configs = [ ":public_include_path" ]
72  all_dependent_configs = [ ":linker_script" ]
73  public_deps = [
74    ":config",
75    dir_pw_preprocessor,
76  ]
77  deps = [ dir_pw_varint ]
78  public = [
79    "public/pw_tokenizer/encode_args.h",
80    "public/pw_tokenizer/hash.h",
81    "public/pw_tokenizer/tokenize.h",
82  ]
83  sources = [
84    "encode_args.cc",
85    "hash.cc",
86    "public/pw_tokenizer/internal/argument_types.h",
87    "public/pw_tokenizer/internal/argument_types_macro_4_byte.h",
88    "public/pw_tokenizer/internal/argument_types_macro_8_byte.h",
89    "public/pw_tokenizer/internal/pw_tokenizer_65599_fixed_length_128_hash_macro.h",
90    "public/pw_tokenizer/internal/pw_tokenizer_65599_fixed_length_256_hash_macro.h",
91    "public/pw_tokenizer/internal/pw_tokenizer_65599_fixed_length_80_hash_macro.h",
92    "public/pw_tokenizer/internal/pw_tokenizer_65599_fixed_length_96_hash_macro.h",
93    "public/pw_tokenizer/internal/tokenize_string.h",
94    "tokenize.cc",
95  ]
96  friend = [ ":*" ]
97}
98
99# As a temporary workaround, if no backend is set, use an empty test backend so
100# that the test can define the handler function.
101# TODO(hepler): Switch this to a facade test when available.
102if (pw_tokenizer_GLOBAL_HANDLER_BACKEND == "" &&
103    pw_tokenizer_GLOBAL_HANDLER_WITH_PAYLOAD_BACKEND == "") {
104  # This is an empty library to use as the backend for global_handler and
105  # global_handler_with_payload tests.
106  pw_source_set("test_backend") {
107    visibility = [ ":*" ]
108  }
109
110  pw_tokenizer_GLOBAL_HANDLER_BACKEND = ":test_backend"
111  pw_tokenizer_GLOBAL_HANDLER_WITH_PAYLOAD_BACKEND = ":test_backend"
112
113  enable_global_handler_test = true
114} else {
115  enable_global_handler_test = false
116}
117
118pw_facade("global_handler") {
119  backend = pw_tokenizer_GLOBAL_HANDLER_BACKEND
120
121  public_configs = [ ":public_include_path" ]
122  public = [ "public/pw_tokenizer/tokenize_to_global_handler.h" ]
123  sources = [ "tokenize_to_global_handler.cc" ]
124  public_deps = [ ":pw_tokenizer" ]
125}
126
127pw_facade("global_handler_with_payload") {
128  backend = pw_tokenizer_GLOBAL_HANDLER_WITH_PAYLOAD_BACKEND
129
130  public_configs = [ ":public_include_path" ]
131  public = [ "public/pw_tokenizer/tokenize_to_global_handler_with_payload.h" ]
132  sources = [ "tokenize_to_global_handler_with_payload.cc" ]
133  public_deps = [ ":pw_tokenizer" ]
134}
135
136pw_source_set("base64") {
137  public_configs = [ ":public_include_path" ]
138  public = [ "public/pw_tokenizer/base64.h" ]
139  sources = [ "base64.cc" ]
140  public_deps = [
141    ":pw_tokenizer",
142    dir_pw_base64,
143    dir_pw_preprocessor,
144  ]
145}
146
147pw_source_set("decoder") {
148  public_configs = [ ":public_include_path" ]
149  deps = [ dir_pw_varint ]
150  public = [
151    "public/pw_tokenizer/detokenize.h",
152    "public/pw_tokenizer/token_database.h",
153  ]
154  sources = [
155    "decode.cc",
156    "detokenize.cc",
157    "public/pw_tokenizer/internal/decode.h",
158    "token_database.cc",
159  ]
160  friend = [ ":*" ]
161}
162
163# Executable for generating test data for the C++ and Python detokenizers. This
164# target should only be built for the host.
165pw_executable("generate_decoding_test_data") {
166  deps = [
167    ":decoder",
168    ":pw_tokenizer",
169    dir_pw_varint,
170  ]
171  sources = [ "generate_decoding_test_data.cc" ]
172}
173
174# Executable for generating a test ELF file for elf_reader_test.py. A host
175# version of this binary is checked in for use in elf_reader_test.py.
176pw_executable("elf_reader_test_binary") {
177  deps = [
178    ":pw_tokenizer",
179    "$dir_pw_varint",
180  ]
181  sources = [ "py/elf_reader_test_binary.c" ]
182  ldflags = [ "-Wl,--unresolved-symbols=ignore-all" ]  # main is not defined
183}
184
185pw_test_group("tests") {
186  tests = [
187    ":argument_types_test",
188    ":base64_test",
189    ":decode_test",
190    ":detokenize_fuzzer",
191    ":detokenize_test",
192    ":global_handlers_test",
193    ":hash_test",
194    ":simple_tokenize_test_cpp11",
195    ":simple_tokenize_test_cpp14",
196    ":simple_tokenize_test_cpp17",
197    ":token_database_fuzzer",
198    ":token_database_test",
199    ":tokenize_test",
200  ]
201  group_deps = [ "$dir_pw_preprocessor:tests" ]
202}
203
204pw_test("argument_types_test") {
205  sources = [
206    "argument_types_test.cc",
207    "argument_types_test_c.c",
208    "pw_tokenizer_private/argument_types_test.h",
209  ]
210  deps = [ ":pw_tokenizer" ]
211
212  if (pw_arduino_build_CORE_PATH != "") {
213    remove_configs = [ "$dir_pw_build:strict_warnings" ]
214  }
215}
216
217pw_test("base64_test") {
218  sources = [ "base64_test.cc" ]
219  deps = [ ":base64" ]
220}
221
222pw_test("decode_test") {
223  sources = [
224    "decode_test.cc",
225    "pw_tokenizer_private/tokenized_string_decoding_test_data.h",
226    "pw_tokenizer_private/varint_decoding_test_data.h",
227  ]
228  deps = [
229    ":decoder",
230    "$dir_pw_varint",
231  ]
232
233  # TODO(tonymd): This fails on Teensyduino 1.54 beta core. It may be related to
234  # linking in stl functions. Will debug when 1.54 is released.
235  enable_if = pw_build_EXECUTABLE_TARGET_TYPE != "arduino_executable"
236}
237
238pw_test("detokenize_test") {
239  sources = [ "detokenize_test.cc" ]
240  deps = [ ":decoder" ]
241
242  # TODO(tonymd): This fails on Teensyduino 1.54 beta core. It may be related to
243  # linking in stl functions. Will debug when 1.54 is released.
244  enable_if = pw_build_EXECUTABLE_TARGET_TYPE != "arduino_executable"
245}
246
247pw_test("global_handlers_test") {
248  sources = [
249    "global_handlers_test.cc",
250    "global_handlers_test_c.c",
251    "pw_tokenizer_private/tokenize_test.h",
252  ]
253  deps = [
254    ":global_handler",
255    ":global_handler_with_payload",
256  ]
257
258  # TODO(hepler): Switch this to a facade test when available.
259  enable_if = enable_global_handler_test
260}
261
262pw_test("hash_test") {
263  sources = [
264    "hash_test.cc",
265    "pw_tokenizer_private/generated_hash_test_cases.h",
266  ]
267  deps = [ ":pw_tokenizer" ]
268}
269
270# Fully test C++11 and C++14 compatibility by compiling all sources as C++11 or
271# C++14.
272_simple_tokenize_test_sources = [
273  "$dir_pw_varint/public/pw_varint/varint.h",
274  "$dir_pw_varint/varint.cc",
275  "encode_args.cc",
276  "public/pw_tokenizer/config.h",
277  "public/pw_tokenizer/encode_args.h",
278  "public/pw_tokenizer/hash.h",
279  "public/pw_tokenizer/internal/argument_types.h",
280  "public/pw_tokenizer/internal/argument_types_macro_4_byte.h",
281  "public/pw_tokenizer/internal/argument_types_macro_8_byte.h",
282  "public/pw_tokenizer/internal/pw_tokenizer_65599_fixed_length_128_hash_macro.h",
283  "public/pw_tokenizer/internal/pw_tokenizer_65599_fixed_length_80_hash_macro.h",
284  "public/pw_tokenizer/internal/pw_tokenizer_65599_fixed_length_96_hash_macro.h",
285  "public/pw_tokenizer/internal/tokenize_string.h",
286  "public/pw_tokenizer/tokenize.h",
287  "public/pw_tokenizer/tokenize_to_global_handler.h",
288  "public/pw_tokenizer/tokenize_to_global_handler_with_payload.h",
289  "simple_tokenize_test.cc",
290  "tokenize.cc",
291  "tokenize_to_global_handler.cc",
292  "tokenize_to_global_handler_with_payload.cc",
293]
294_simple_tokenize_test_configs = [
295  ":public_include_path",
296  "$dir_pw_varint:default_config",
297]
298
299pw_test("simple_tokenize_test_cpp11") {
300  remove_configs = [ "$dir_pw_build:cpp17" ]
301  configs = [ "$dir_pw_build:cpp11" ] + _simple_tokenize_test_configs
302  sources = _simple_tokenize_test_sources
303  deps = [ dir_pw_preprocessor ]
304}
305
306pw_test("simple_tokenize_test_cpp14") {
307  remove_configs = [ "$dir_pw_build:cpp17" ]
308  configs = [ "$dir_pw_build:cpp14" ] + _simple_tokenize_test_configs
309  sources = _simple_tokenize_test_sources
310  deps = [ dir_pw_preprocessor ]
311}
312
313pw_test("simple_tokenize_test_cpp17") {
314  configs = _simple_tokenize_test_configs
315  sources = _simple_tokenize_test_sources
316  deps = [ dir_pw_preprocessor ]
317}
318
319pw_test("token_database_test") {
320  sources = [ "token_database_test.cc" ]
321  deps = [ ":decoder" ]
322}
323
324pw_test("tokenize_test") {
325  sources = [
326    "pw_tokenizer_private/tokenize_test.h",
327    "tokenize_test.cc",
328    "tokenize_test_c.c",
329  ]
330  deps = [
331    ":pw_tokenizer",
332    "$dir_pw_varint",
333  ]
334}
335
336pw_fuzzer("token_database_fuzzer") {
337  sources = [ "token_database_fuzzer.cc" ]
338  deps = [
339    ":decoder",
340    "$dir_pw_fuzzer",
341    "$dir_pw_preprocessor",
342  ]
343}
344
345pw_fuzzer("detokenize_fuzzer") {
346  sources = [ "detokenize_fuzzer.cc" ]
347  deps = [
348    ":decoder",
349    "$dir_pw_fuzzer",
350    "$dir_pw_preprocessor",
351  ]
352}
353
354declare_args() {
355  # pw_JAVA_NATIVE_INTERFACE_INCLUDE_DIRS specifies the paths to use for
356  # building Java Native Interface libraries. If no paths are provided, targets
357  # that require JNI may not build correctly.
358  #
359  # Example JNI include paths for a Linux system:
360  #
361  #   pw_JAVA_NATIVE_INTERFACE_INCLUDE_DIRS = [
362  #     "/usr/local/buildtools/java/jdk/include/",
363  #     "/usr/local/buildtools/java/jdk/include/linux",
364  #   ]
365  #
366  pw_JAVA_NATIVE_INTERFACE_INCLUDE_DIRS = []
367}
368
369# Create a shared library for the tokenizer JNI wrapper. The include paths for
370# the JNI headers must be available in the system or provided with the
371# pw_JAVA_NATIVE_INTERFACE_INCLUDE_DIRS variable.
372pw_shared_library("detokenizer_jni") {
373  public_configs = [ ":public_include_path" ]
374  include_dirs = pw_JAVA_NATIVE_INTERFACE_INCLUDE_DIRS
375  sources = [ "java/dev/pigweed/tokenizer/detokenizer.cc" ]
376  public_deps = [
377    ":decoder",
378    "$dir_pw_preprocessor",
379  ]
380}
381
382pw_doc_group("docs") {
383  sources = [ "docs.rst" ]
384  inputs = [ "py/pw_tokenizer/encode.py" ]
385}
386