1add_subdirectory(memory_utils)
2
3add_header_library(
4  string_utils
5  HDRS
6    string_utils.h
7  DEPENDS
8    libc.utils.CPP.standalone_cpp
9)
10
11add_entrypoint_object(
12  strcat
13  SRCS
14    strcat.cpp
15  HDRS
16    strcat.h
17  DEPENDS
18    .strcpy
19    .string_utils
20)
21
22add_entrypoint_object(
23  strcpy
24  SRCS
25    strcpy.cpp
26  HDRS
27    strcpy.h
28  DEPENDS
29    .memcpy
30    .string_utils
31)
32
33add_entrypoint_object(
34  strlen
35  SRCS
36    strlen.cpp
37  HDRS
38    strlen.h
39  DEPENDS
40    libc.include.string
41)
42
43add_entrypoint_object(
44  strcmp
45  SRCS
46    strcmp.cpp
47  HDRS
48    strcmp.h
49)
50
51add_entrypoint_object(
52  memchr
53  SRCS
54    memchr.cpp
55  HDRS
56    memchr.h
57  DEPENDS
58    .string_utils
59)
60
61add_entrypoint_object(
62  strchr
63  SRCS
64    strchr.cpp
65  HDRS
66    strchr.h
67)
68
69add_entrypoint_object(
70  strstr
71  SRCS
72    strstr.cpp
73  HDRS
74    strstr.h
75)
76
77add_entrypoint_object(
78  strncpy
79  SRCS
80    strncpy.cpp
81  HDRS
82    strncpy.h
83)
84
85add_entrypoint_object(
86  strnlen
87  SRCS
88    strnlen.cpp
89  HDRS
90    strnlen.h
91  DEPENDS
92    .string_utils
93)
94
95add_entrypoint_object(
96  memrchr
97  SRCS
98    memrchr.cpp
99  HDRS
100    memrchr.h
101)
102
103add_entrypoint_object(
104  strrchr
105  SRCS
106    strrchr.cpp
107  HDRS
108    strrchr.h
109)
110
111add_entrypoint_object(
112  strcspn
113  SRCS
114    strcspn.cpp
115  HDRS
116    strcspn.h
117  DEPENDS
118    .string_utils
119)
120
121add_entrypoint_object(
122  strspn
123  SRCS
124    strspn.cpp
125  HDRS
126    strspn.h
127  DEPENDS
128    libc.utils.CPP.standalone_cpp
129)
130
131add_entrypoint_object(
132  strpbrk
133  SRCS
134    strpbrk.cpp
135  HDRS
136    strpbrk.h
137  DEPENDS
138    .string_utils
139)
140
141add_entrypoint_object(
142  strtok
143  SRCS
144    strtok.cpp
145  HDRS
146    strtok.h
147  DEPENDS
148    .string_utils
149)
150
151add_entrypoint_object(
152  strtok_r
153  SRCS
154    strtok_r.cpp
155  HDRS
156    strtok_r.h
157  DEPENDS
158    .string_utils
159)
160
161# Helper to define a function with multiple implementations
162# - Computes flags to satisfy required/rejected features and arch,
163# - Declares an entry point,
164# - Attach the REQUIRE_CPU_FEATURES property to the target,
165# - Add the fully qualified target to `${name}_implementations` global property for tests.
166function(add_implementation name impl_name)
167  cmake_parse_arguments(
168    "ADD_IMPL"
169    "" # Optional arguments
170    "MARCH" # Single value arguments
171    "REQUIRE;REJECT;SRCS;HDRS;DEPENDS;COMPILE_OPTIONS" # Multi value arguments
172    ${ARGN})
173  compute_flags(flags
174    MARCH ${ADD_IMPL_MARCH}
175    REQUIRE ${ADD_IMPL_REQUIRE}
176    REJECT ${ADD_IMPL_REJECT}
177  )
178  add_entrypoint_object(${impl_name}
179    NAME ${name}
180    SRCS ${ADD_IMPL_SRCS}
181    HDRS ${ADD_IMPL_HDRS}
182    DEPENDS ${ADD_IMPL_DEPENDS}
183    COMPILE_OPTIONS ${ADD_IMPL_COMPILE_OPTIONS} ${flags} -O2
184  )
185  get_fq_target_name(${impl_name} fq_target_name)
186  set_target_properties(${fq_target_name} PROPERTIES REQUIRE_CPU_FEATURES "${ADD_IMPL_REQUIRE}")
187  set_property(GLOBAL APPEND PROPERTY "${name}_implementations" "${fq_target_name}")
188endfunction()
189
190# ------------------------------------------------------------------------------
191# memcpy
192# ------------------------------------------------------------------------------
193
194# include the relevant architecture specific implementations
195if(${LIBC_TARGET_MACHINE} STREQUAL "x86_64")
196  set(LIBC_STRING_TARGET_ARCH "x86")
197  set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/x86/memcpy.cpp)
198else()
199  set(LIBC_STRING_TARGET_ARCH ${LIBC_TARGET_MACHINE})
200  set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/memcpy.cpp)
201endif()
202
203function(add_memcpy memcpy_name)
204  add_implementation(memcpy ${memcpy_name}
205    SRCS ${MEMCPY_SRC}
206    HDRS ${LIBC_SOURCE_DIR}/src/string/memcpy.h
207    DEPENDS
208      .memory_utils.memory_utils
209      libc.include.string
210    COMPILE_OPTIONS
211      -fno-builtin-memcpy
212    ${ARGN}
213  )
214endfunction()
215
216if(${LIBC_STRING_TARGET_ARCH} STREQUAL "x86")
217  add_memcpy(memcpy MARCH native)
218else()
219  add_memcpy(memcpy)
220endif()
221
222# ------------------------------------------------------------------------------
223# memset
224# ------------------------------------------------------------------------------
225
226function(add_memset memset_name)
227  add_implementation(memset ${memset_name}
228    SRCS ${LIBC_SOURCE_DIR}/src/string/memset.cpp
229    HDRS ${LIBC_SOURCE_DIR}/src/string/memset.h
230    DEPENDS
231      .memory_utils.memory_utils
232      libc.include.string
233    COMPILE_OPTIONS
234      -fno-builtin-memset
235    ${ARGN}
236  )
237endfunction()
238
239if(${LIBC_STRING_TARGET_ARCH} STREQUAL "x86")
240  add_memset(memset MARCH native)
241else()
242  add_memset(memset)
243endif()
244
245# ------------------------------------------------------------------------------
246# bzero
247# ------------------------------------------------------------------------------
248
249function(add_bzero bzero_name)
250  add_implementation(bzero ${bzero_name}
251    SRCS ${LIBC_SOURCE_DIR}/src/string/bzero.cpp
252    HDRS ${LIBC_SOURCE_DIR}/src/string/bzero.h
253    DEPENDS
254      .memory_utils.memory_utils
255      libc.include.string
256    COMPILE_OPTIONS
257      -fno-builtin-memset
258      -fno-builtin-bzero
259    ${ARGN}
260  )
261endfunction()
262
263if(${LIBC_STRING_TARGET_ARCH} STREQUAL "x86")
264  add_bzero(bzero MARCH native)
265else()
266  add_bzero(bzero)
267endif()
268
269# ------------------------------------------------------------------------------
270# Add all other relevant implementations for the native target.
271# ------------------------------------------------------------------------------
272
273if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_STRING_TARGET_ARCH})
274  include(${LIBC_STRING_TARGET_ARCH}/CMakeLists.txt)
275endif()
276