xref: /aosp_15_r20/external/pigweed/third_party/emboss/emboss.cmake (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker# Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker#
3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker# the License at
6*61c4878aSAndroid Build Coastguard Worker#
7*61c4878aSAndroid Build Coastguard Worker#     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker#
9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker# the License.
14*61c4878aSAndroid Build Coastguard Workerinclude_guard(GLOBAL)
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard Workerinclude($ENV{PW_ROOT}/pw_build/pigweed.cmake)
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Workerfunction(emboss_cc_library NAME)
19*61c4878aSAndroid Build Coastguard Worker  pw_parse_arguments(
20*61c4878aSAndroid Build Coastguard Worker    NUM_POSITIONAL_ARGS
21*61c4878aSAndroid Build Coastguard Worker      1
22*61c4878aSAndroid Build Coastguard Worker    ONE_VALUE_ARGS
23*61c4878aSAndroid Build Coastguard Worker      SOURCES
24*61c4878aSAndroid Build Coastguard Worker    MULTI_VALUE_ARGS
25*61c4878aSAndroid Build Coastguard Worker      IMPORT_DIRS
26*61c4878aSAndroid Build Coastguard Worker      DEPS
27*61c4878aSAndroid Build Coastguard Worker    REQUIRED_ARGS
28*61c4878aSAndroid Build Coastguard Worker      SOURCES)
29*61c4878aSAndroid Build Coastguard Worker
30*61c4878aSAndroid Build Coastguard Worker  # Include default import dirs with the user specified values.
31*61c4878aSAndroid Build Coastguard Worker  list(APPEND arg_IMPORT_DIRS $ENV{PW_ROOT} ${CMAKE_CURRENT_SOURCE_DIR})
32*61c4878aSAndroid Build Coastguard Worker
33*61c4878aSAndroid Build Coastguard Worker  set(out_dir "${CMAKE_CURRENT_BINARY_DIR}/${NAME}")
34*61c4878aSAndroid Build Coastguard Worker
35*61c4878aSAndroid Build Coastguard Worker  # embossc will output and emb to: <output_path>/<input_path>.emb.h
36*61c4878aSAndroid Build Coastguard Worker  pw_rebase_paths(outputs ${out_dir} "${CMAKE_CURRENT_SOURCE_DIR}"
37*61c4878aSAndroid Build Coastguard Worker    "${arg_SOURCES}" ".emb.h")
38*61c4878aSAndroid Build Coastguard Worker
39*61c4878aSAndroid Build Coastguard Worker  # Set the include path to export to the output file's directory.
40*61c4878aSAndroid Build Coastguard Worker  get_filename_component(output_include_path "${outputs}" DIRECTORY)
41*61c4878aSAndroid Build Coastguard Worker
42*61c4878aSAndroid Build Coastguard Worker  # Make the import dir paths absolute
43*61c4878aSAndroid Build Coastguard Worker  pw_rebase_paths(abs_import_dirs "${CMAKE_CURRENT_SOURCE_DIR}"
44*61c4878aSAndroid Build Coastguard Worker    "${CMAKE_CURRENT_SOURCE_DIR}" "${arg_IMPORT_DIRS}" "")
45*61c4878aSAndroid Build Coastguard Worker
46*61c4878aSAndroid Build Coastguard Worker  # Expose a list of our sources so that other generate steps can depend on
47*61c4878aSAndroid Build Coastguard Worker  # them.
48*61c4878aSAndroid Build Coastguard Worker  pw_rebase_paths(abs_sources "${CMAKE_CURRENT_SOURCE_DIR}"
49*61c4878aSAndroid Build Coastguard Worker    "${CMAKE_CURRENT_SOURCE_DIR}" "${arg_SOURCES}" "")
50*61c4878aSAndroid Build Coastguard Worker  pw_add_library_generic("${NAME}._sources" INTERFACE
51*61c4878aSAndroid Build Coastguard Worker    HEADERS
52*61c4878aSAndroid Build Coastguard Worker      ${abs_sources}
53*61c4878aSAndroid Build Coastguard Worker    PUBLIC_INCLUDES
54*61c4878aSAndroid Build Coastguard Worker      ${abs_import_dirs}
55*61c4878aSAndroid Build Coastguard Worker  )
56*61c4878aSAndroid Build Coastguard Worker
57*61c4878aSAndroid Build Coastguard Worker  # Build up a list of other `emb` sources the generate step depends on. We
58*61c4878aSAndroid Build Coastguard Worker  # use this rather than the full emboss_cc_library so that the generate steps
59*61c4878aSAndroid Build Coastguard Worker  # can run in parallel.
60*61c4878aSAndroid Build Coastguard Worker  set(source_deps "${arg_DEPS}")
61*61c4878aSAndroid Build Coastguard Worker  list(TRANSFORM source_deps APPEND ._sources)
62*61c4878aSAndroid Build Coastguard Worker
63*61c4878aSAndroid Build Coastguard Worker  # We need to extract the actual files from the targets for them to work
64*61c4878aSAndroid Build Coastguard Worker  # as proper dependencies for `add_custom_command`.
65*61c4878aSAndroid Build Coastguard Worker  set(dependent_sources "")
66*61c4878aSAndroid Build Coastguard Worker  foreach(dep IN LISTS source_deps)
67*61c4878aSAndroid Build Coastguard Worker    get_target_property(sources ${dep} SOURCES)
68*61c4878aSAndroid Build Coastguard Worker    list(APPEND dependent_sources ${sources})
69*61c4878aSAndroid Build Coastguard Worker    get_target_property(
70*61c4878aSAndroid Build Coastguard Worker      imports ${dep}._public_config INTERFACE_INCLUDE_DIRECTORIES)
71*61c4878aSAndroid Build Coastguard Worker    list(APPEND abs_import_dirs ${imports})
72*61c4878aSAndroid Build Coastguard Worker  endforeach()
73*61c4878aSAndroid Build Coastguard Worker
74*61c4878aSAndroid Build Coastguard Worker  # Setup the emboss command:
75*61c4878aSAndroid Build Coastguard Worker  # python3 $runner $embossc --generate cc --output-path $out_dir \
76*61c4878aSAndroid Build Coastguard Worker  # --import-dir ... --import-dir ... $source
77*61c4878aSAndroid Build Coastguard Worker  set(embossc "${dir_pw_third_party_emboss}/embossc")
78*61c4878aSAndroid Build Coastguard Worker  set(runner "$ENV{PW_ROOT}/third_party/emboss/embossc_runner.py")
79*61c4878aSAndroid Build Coastguard Worker
80*61c4878aSAndroid Build Coastguard Worker  list(APPEND emboss_cmd python3 "-OO"
81*61c4878aSAndroid Build Coastguard Worker    "${runner}" "${embossc}" "--generate" "cc" "--no-cc-enum-traits"
82*61c4878aSAndroid Build Coastguard Worker    "--output-path" "${out_dir}")
83*61c4878aSAndroid Build Coastguard Worker
84*61c4878aSAndroid Build Coastguard Worker  foreach(impt IN LISTS abs_import_dirs)
85*61c4878aSAndroid Build Coastguard Worker    list(APPEND emboss_cmd "--import-dir" "${impt}")
86*61c4878aSAndroid Build Coastguard Worker  endforeach()
87*61c4878aSAndroid Build Coastguard Worker
88*61c4878aSAndroid Build Coastguard Worker  list(APPEND emboss_cmd "${arg_SOURCES}")
89*61c4878aSAndroid Build Coastguard Worker
90*61c4878aSAndroid Build Coastguard Worker  # Define the command to generate $outputs
91*61c4878aSAndroid Build Coastguard Worker  add_custom_command(
92*61c4878aSAndroid Build Coastguard Worker    COMMAND
93*61c4878aSAndroid Build Coastguard Worker      ${emboss_cmd}
94*61c4878aSAndroid Build Coastguard Worker    DEPENDS
95*61c4878aSAndroid Build Coastguard Worker      ${runner}
96*61c4878aSAndroid Build Coastguard Worker      ${arg_SOURCES}
97*61c4878aSAndroid Build Coastguard Worker      ${dependent_sources}
98*61c4878aSAndroid Build Coastguard Worker    OUTPUT
99*61c4878aSAndroid Build Coastguard Worker      ${outputs})
100*61c4878aSAndroid Build Coastguard Worker  # Tie a target to $outputs that will trigger the command
101*61c4878aSAndroid Build Coastguard Worker  add_custom_target("${NAME}._generate" DEPENDS ${outputs})
102*61c4878aSAndroid Build Coastguard Worker
103*61c4878aSAndroid Build Coastguard Worker  # Export a library that exposes the generated outputs
104*61c4878aSAndroid Build Coastguard Worker  pw_add_library_generic("${NAME}" INTERFACE
105*61c4878aSAndroid Build Coastguard Worker    PUBLIC_INCLUDES
106*61c4878aSAndroid Build Coastguard Worker      "${out_dir}/public"
107*61c4878aSAndroid Build Coastguard Worker      "${output_include_path}"
108*61c4878aSAndroid Build Coastguard Worker    PUBLIC_DEPS
109*61c4878aSAndroid Build Coastguard Worker      pw_third_party.emboss.cpp_utils
110*61c4878aSAndroid Build Coastguard Worker      ${arg_DEPS})
111*61c4878aSAndroid Build Coastguard Worker  # Tie in the generated outputs as a dep of the library
112*61c4878aSAndroid Build Coastguard Worker  add_dependencies("${NAME}" "${NAME}._generate")
113*61c4878aSAndroid Build Coastguard Workerendfunction(emboss_cc_library)
114