xref: /aosp_15_r20/external/pigweed/pw_tokenizer/database.cmake (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2023 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.
14include_guard(GLOBAL)
15
16include($ENV{PW_ROOT}/pw_build/pigweed.cmake)
17
18# This function creates a library under the specified ${NAME} which provides a
19# generated token database for a given ELF file using pw_tokenizer/database.py.
20#
21# Produces the ${NAME} token database.
22#
23# Args:
24#
25#   NAME - name of the library to create
26#   COMMIT - Deletes temporary tokens in memory and on disk when a CSV exists
27#       within a commit.
28#   CREATE - Create a database. Must be set to one of the supported database
29#       types: "csv" or "binary".
30#   DATABASE - If updating a database, path to an existing database in the
31#       source tree; optional if creating a database, but may provide an output
32#       directory path to override the default of
33#       "$target_gen_dir/$target_name.[csv/binary]"
34#   DEPS - CMake targets to build prior to generating the database; artifacts
35#       from these targets are NOT implicitly used for database generation.
36#   DOMAIN - If provided, extract strings from tokenization domains matching
37#       this regular expression.
38#   TARGET - CMake target (executable or library) from which to add tokens;
39#       this target is also added to deps.
40function(pw_tokenizer_database NAME)
41  pw_parse_arguments(
42    NUM_POSITIONAL_ARGS
43      1
44    ONE_VALUE_ARGS
45      COMMIT
46      CREATE
47      DATABASE
48      DEPS
49      DOMAIN
50      TARGET
51    REQUIRED_ARGS
52      TARGET
53  )
54
55  if(NOT DEFINED arg_CREATE AND NOT DEFINED arg_DATABASE)
56    message(FATAL_ERROR "pw_tokenizer_database requires a `database` "
57            "variable, unless 'CREATE' is specified")
58  endif()
59
60  set(_create "")
61  if(DEFINED arg_CREATE)
62    if (NOT (${arg_CREATE} STREQUAL "csv" OR ${arg_CREATE} STREQUAL "binary"))
63      message(FATAL_ERROR "'CREATE' must be \"csv\" or \"binary\".")
64    endif()
65    set(_create ${arg_CREATE})
66    set(_create_new_database TRUE)
67  endif()
68
69  set(_database "")
70  if(DEFINED arg_DATABASE)
71    set(_database ${arg_DATABASE})
72  else()
73    # Default to appending the create type as the extension.
74    set(_database ${NAME}.${_create})
75  endif()
76
77  set(_domain "")
78  if(DEFINED arg_DOMAIN)
79    set(_domain "#${arg_DOMAIN}")
80  endif()
81
82  add_library(${NAME} INTERFACE)
83  add_dependencies(${NAME} INTERFACE ${NAME}_generated_token_db)
84
85  if (DEFINED _create_new_database)
86    add_custom_command(
87        COMMENT "Generating the ${_database} token database"
88        COMMAND
89          ${Python3_EXECUTABLE}
90          "$ENV{PW_ROOT}/pw_tokenizer/py/pw_tokenizer/database.py" create
91          --database ${_database}
92          --type ${_create}
93          "$<TARGET_FILE:${arg_TARGET}>${_domain}"
94          --force
95        DEPENDS
96          ${arg_DEPS}
97          ${arg_TARGET}
98        OUTPUT ${_database} POST_BUILD
99    )
100  else()
101    set(_discard_temporary "")
102    if(DEFINED arg_COMMIT)
103      set(_discard_temporary "--discard-temporary ${arg_COMMIT}")
104    endif()
105
106    add_custom_command(
107        COMMENT "Updating the ${_database} token database"
108        COMMAND
109          ${Python3_EXECUTABLE}
110          "$ENV{PW_ROOT}/pw_tokenizer/py/pw_tokenizer/database.py" add
111          --database ${_database}
112          ${_discard_temporary}
113          "$<TARGET_FILE:${arg_TARGET}>${_domain}"
114        DEPENDS
115          ${arg_DEPS}
116          ${arg_TARGET}
117        OUTPUT ${_database} POST_BUILD
118    )
119  endif()
120
121  add_custom_target(${NAME}_generated_token_db
122    DEPENDS ${_database}
123  )
124endfunction()
125