xref: /aosp_15_r20/external/pigweed/pw_libc/BUILD.gn (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.
14
15import("//build_overrides/pigweed.gni")
16
17import("$dir_pw_build/target_types.gni")
18import("$dir_pw_docgen/docs.gni")
19import("$dir_pw_third_party/llvm_libc/llvm_libc.gni")
20import("$dir_pw_toolchain/generate_toolchain.gni")
21import("$dir_pw_unit_test/test.gni")
22
23config("default_config") {
24  include_dirs = [ "public" ]
25}
26
27pw_test_group("tests") {
28  tests = [
29    ":llvm_libc_tests",
30    ":memset_test",
31  ]
32}
33
34pw_test("memset_test") {
35  sources = [ "memset_test.cc" ]
36  deps = [ "$dir_pw_containers" ]
37}
38
39# Clang has __attribute__(("no-builtin")), but gcc doesn't support it so we
40# need this flag instead.
41config("no-builtin") {
42  cflags = [ "-fno-builtin" ]
43}
44
45# Downstream projects sometimes build with -Wshadow, which on gcc also warns
46# about constructor arguments shadowing struct members. This is too pedantic
47# and not reasonable to change upstream llvm-libc.
48config("no-shadow") {
49  cflags = [ "-Wno-shadow" ]
50}
51
52# If dir_pw_third_party_llvm_libc is defined, use that directory to create a
53# pw_libc.a from llvm-libc. Otherwise, we create an empty pw_libc.a.
54if (dir_pw_third_party_llvm_libc != "") {
55  pw_libc_source_set("stdlib") {
56    functions = [
57      "abs",
58      "rand",
59      "srand",
60    ]
61    additional_srcs = [
62      "baremetal/abort.cpp",
63      "rand_util.cpp",
64    ]
65
66    # srand and rand are both tested in rand_test.cpp.
67    no_test_functions = [ "srand" ]
68  }
69
70  pw_libc_source_set("string") {
71    defines = [ "LIBC_COPT_MEMCPY_USE_EMBEDDED_TINY" ]
72    functions = [
73      "strcmp",
74      "strcpy",
75      "strstr",
76      "strncpy",
77      "strnlen",
78      "memcpy",
79      "memset",
80      "memmove",
81    ]
82
83    # memmove tests use gtest matchers which pw_unit_test doesn't support.
84    no_test_functions = [ "memmove" ]
85
86    configs = [
87      ":no-builtin",
88      ":no-shadow",
89    ]
90  }
91
92  pw_libc_source_set("ctype") {
93    functions = [ "isprint" ]
94  }
95
96  pw_libc_source_set("time") {
97    functions = [ "gmtime" ]
98    additional_srcs = [ "time_utils.cpp" ]
99
100    # gmtime requires gtest matchers which pw_unit_test doesn't support.
101    # Moreover, the matches in llvm-libc don't have the same internal API that
102    # gtest does, so it isn't possible to enable this tests when using gtest
103    # either.
104    no_test_functions = [ "gmtime" ]
105  }
106
107  pw_libc_source_set("math") {
108    non_cpu_dir = "generic"
109
110    functions = [
111      "modff",
112      "roundf",
113    ]
114
115    # Math tests require the MPFR library, which is not available.
116    no_test_functions = functions
117  }
118
119  pw_libc_source_set("stdio") {
120    functions = [
121      "snprintf",
122      "vsnprintf",
123    ]
124
125    additional_srcs = [
126      "printf_core/printf_main.cpp",
127      "printf_core/writer.cpp",
128      "printf_core/converter.cpp",
129    ]
130
131    defines = [
132      "LIBC_COPT_PRINTF_DISABLE_FLOAT",
133      "LIBC_COPT_PRINTF_DISABLE_WRITE_INT",
134      "LIBC_COPT_PRINTF_DISABLE_INDEX_MODE",
135    ]
136
137    # This config includes -Wshadow. On gcc, this warns even for constructor
138    # arguments which shadow members. This is too pedantic and shouldn't be
139    # changed upstream.
140    remove_configs = [ "//pw_build:extra_strict_warnings" ]
141  }
142
143  pw_libc_source_set("stdfix") {
144    functions = [
145      "expk",
146      "roundlk",
147      "sqrtulr",
148      "sqrtur",
149      "uksqrtui",
150    ]
151    defines = [ "LIBC_FAST_MATH=1" ]
152  }
153
154  pw_static_library("pw_libc") {
155    complete_static_lib = true
156    add_global_link_deps = false
157    deps = [
158      ":ctype",
159      ":math",
160      ":stdio",
161      ":stdlib",
162      ":string",
163      ":time",
164    ]
165  }
166
167  pw_test_group("llvm_libc_tests") {
168    tests = [
169      ":ctype_tests",
170      ":math_tests",
171      ":stdio_tests",
172      ":stdlib_tests",
173      ":string_tests",
174      ":time_tests",
175    ]
176  }
177
178  pw_static_library("pw_libc_stdfix") {
179    complete_static_lib = true
180    add_global_link_deps = false
181    deps = [
182      ":stdfix",
183      ":stdio",
184    ]
185  }
186} else {
187  pw_static_library("pw_libc") {
188    add_global_link_deps = false
189  }
190
191  pw_static_library("pw_libc_stdfix") {
192    add_global_link_deps = false
193  }
194
195  pw_test_group("llvm_libc_tests") {
196  }
197}
198
199pw_doc_group("docs") {
200  sources = [ "docs.rst" ]
201}
202