blob: 00dda09a2490bb606e774b646a98519d1b430542 [file] [log] [blame]
# Copyright 2025 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Generates a C source file containing absolute symbols from autoconf.h.
While autoconf.h contains C macros defining the Kconfig symbol values, the
build system sometimes need the symbols defined in assembly. This script
converts the preprocessor macros to macros defining the symbols in assembly.
"""
import argparse
import pathlib
import re
def process_configs(
autoconf: pathlib.Path,
output: pathlib.Path,
) -> None:
with open(autoconf, 'r', encoding='utf-8') as input_autoconf:
with open(output, 'w', encoding='utf-8') as output_c_file:
output_c_file.write("""/* file is auto-generated, do not modify ! */
#include <zephyr/toolchain.h>
GEN_ABS_SYM_BEGIN (_ConfigAbsSyms)
""")
for line in input_autoconf:
if not line.startswith('#define CONFIG_'):
continue
line = line.strip()
# Take the symbol name and symbol value and give them to the
# GEN_ABSOLUTE_SYM_KCONFIG() macro, which generates absolute
# symbols in assembly. This is used for the kernel to detect
# the Kconfig results.
#
# This matches the CMake version in native Zephyr.
transformed_line = re.sub(
r'#define (CONFIG_[A-Za-z_0-9]*) (.*)',
r'GEN_ABSOLUTE_SYM_KCONFIG(\1, \2)',
line,
)
# Replace string values with integer 1. It's not obvious why
# this is done in Zephyr, but this is likely to make the
# GEN_ABSOLUTE_SYM_KCONFIG() macro simpler.
transformed_line = re.sub(r'"(.*)"', '1', transformed_line)
output_c_file.write(transformed_line)
output_c_file.write(';\n')
output_c_file.write('GEN_ABS_SYM_END')
def main() -> None:
parser = argparse.ArgumentParser(
description='Generate config.c from autoconf.h'
)
parser.add_argument(
'--autoconf',
type=pathlib.Path,
help='autoconf.h to load',
required=True,
)
parser.add_argument(
'-o',
type=pathlib.Path,
help='Output file',
required=True,
)
args = parser.parse_args()
process_configs(args.autoconf, args.o)
if __name__ == '__main__':
main()