xref: /aosp_15_r20/external/pigweed/third_party/emboss/emboss.cmake (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2024 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
18function(emboss_cc_library NAME)
19  pw_parse_arguments(
20    NUM_POSITIONAL_ARGS
21      1
22    ONE_VALUE_ARGS
23      SOURCES
24    MULTI_VALUE_ARGS
25      IMPORT_DIRS
26      DEPS
27    REQUIRED_ARGS
28      SOURCES)
29
30  # Include default import dirs with the user specified values.
31  list(APPEND arg_IMPORT_DIRS $ENV{PW_ROOT} ${CMAKE_CURRENT_SOURCE_DIR})
32
33  set(out_dir "${CMAKE_CURRENT_BINARY_DIR}/${NAME}")
34
35  # embossc will output and emb to: <output_path>/<input_path>.emb.h
36  pw_rebase_paths(outputs ${out_dir} "${CMAKE_CURRENT_SOURCE_DIR}"
37    "${arg_SOURCES}" ".emb.h")
38
39  # Set the include path to export to the output file's directory.
40  get_filename_component(output_include_path "${outputs}" DIRECTORY)
41
42  # Make the import dir paths absolute
43  pw_rebase_paths(abs_import_dirs "${CMAKE_CURRENT_SOURCE_DIR}"
44    "${CMAKE_CURRENT_SOURCE_DIR}" "${arg_IMPORT_DIRS}" "")
45
46  # Expose a list of our sources so that other generate steps can depend on
47  # them.
48  pw_rebase_paths(abs_sources "${CMAKE_CURRENT_SOURCE_DIR}"
49    "${CMAKE_CURRENT_SOURCE_DIR}" "${arg_SOURCES}" "")
50  pw_add_library_generic("${NAME}._sources" INTERFACE
51    HEADERS
52      ${abs_sources}
53    PUBLIC_INCLUDES
54      ${abs_import_dirs}
55  )
56
57  # Build up a list of other `emb` sources the generate step depends on. We
58  # use this rather than the full emboss_cc_library so that the generate steps
59  # can run in parallel.
60  set(source_deps "${arg_DEPS}")
61  list(TRANSFORM source_deps APPEND ._sources)
62
63  # We need to extract the actual files from the targets for them to work
64  # as proper dependencies for `add_custom_command`.
65  set(dependent_sources "")
66  foreach(dep IN LISTS source_deps)
67    get_target_property(sources ${dep} SOURCES)
68    list(APPEND dependent_sources ${sources})
69    get_target_property(
70      imports ${dep}._public_config INTERFACE_INCLUDE_DIRECTORIES)
71    list(APPEND abs_import_dirs ${imports})
72  endforeach()
73
74  # Setup the emboss command:
75  # python3 $runner $embossc --generate cc --output-path $out_dir \
76  # --import-dir ... --import-dir ... $source
77  set(embossc "${dir_pw_third_party_emboss}/embossc")
78  set(runner "$ENV{PW_ROOT}/third_party/emboss/embossc_runner.py")
79
80  list(APPEND emboss_cmd python3 "-OO"
81    "${runner}" "${embossc}" "--generate" "cc" "--no-cc-enum-traits"
82    "--output-path" "${out_dir}")
83
84  foreach(impt IN LISTS abs_import_dirs)
85    list(APPEND emboss_cmd "--import-dir" "${impt}")
86  endforeach()
87
88  list(APPEND emboss_cmd "${arg_SOURCES}")
89
90  # Define the command to generate $outputs
91  add_custom_command(
92    COMMAND
93      ${emboss_cmd}
94    DEPENDS
95      ${runner}
96      ${arg_SOURCES}
97      ${dependent_sources}
98    OUTPUT
99      ${outputs})
100  # Tie a target to $outputs that will trigger the command
101  add_custom_target("${NAME}._generate" DEPENDS ${outputs})
102
103  # Export a library that exposes the generated outputs
104  pw_add_library_generic("${NAME}" INTERFACE
105    PUBLIC_INCLUDES
106      "${out_dir}/public"
107      "${output_include_path}"
108    PUBLIC_DEPS
109      pw_third_party.emboss.cpp_utils
110      ${arg_DEPS})
111  # Tie in the generated outputs as a dep of the library
112  add_dependencies("${NAME}" "${NAME}._generate")
113endfunction(emboss_cc_library)
114