1# This directory contains a large amount of C code which provides
2# generic implementations of the core runtime library along with optimized
3# architecture-specific code in various subdirectories.
4
5if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
6  cmake_minimum_required(VERSION 3.4.3)
7
8  project(CompilerRTBuiltins C ASM)
9  set(COMPILER_RT_STANDALONE_BUILD TRUE)
10  set(COMPILER_RT_BUILTINS_STANDALONE_BUILD TRUE)
11  list(INSERT CMAKE_MODULE_PATH 0
12    "${CMAKE_SOURCE_DIR}/../../cmake"
13    "${CMAKE_SOURCE_DIR}/../../cmake/Modules")
14  include(base-config-ix)
15  include(CompilerRTUtils)
16
17  load_llvm_config()
18  construct_compiler_rt_default_triple()
19
20  if(APPLE)
21    include(CompilerRTDarwinUtils)
22  endif()
23  if(CMAKE_HOST_APPLE AND APPLE)
24    include(UseLibtool)
25  endif()
26  include(AddCompilerRT)
27endif()
28
29if (COMPILER_RT_STANDALONE_BUILD)
30  # When compiler-rt is being built standalone, possibly as a cross-compilation
31  # target, the target may or may not want position independent code. This
32  # option provides an avenue through which the flag may be controlled when an
33  # LLVM configuration is not being utilized.
34  option(COMPILER_RT_BUILTINS_ENABLE_PIC
35    "Turns on or off -fPIC for the builtin library source"
36    ON)
37endif()
38
39include(builtin-config-ix)
40
41# TODO: Need to add a mechanism for logging errors when builtin source files are
42# added to a sub-directory and not this CMakeLists file.
43set(GENERIC_SOURCES
44  absvdi2.c
45  absvsi2.c
46  absvti2.c
47  adddf3.c
48  addsf3.c
49  addvdi3.c
50  addvsi3.c
51  addvti3.c
52  apple_versioning.c
53  ashldi3.c
54  ashlti3.c
55  ashrdi3.c
56  ashrti3.c
57  bswapdi2.c
58  bswapsi2.c
59  clzdi2.c
60  clzsi2.c
61  clzti2.c
62  cmpdi2.c
63  cmpti2.c
64  comparedf2.c
65  comparesf2.c
66  ctzdi2.c
67  ctzsi2.c
68  ctzti2.c
69  divdc3.c
70  divdf3.c
71  divdi3.c
72  divmoddi4.c
73  divmodsi4.c
74  divsc3.c
75  divsf3.c
76  divsi3.c
77  divti3.c
78  extendsfdf2.c
79  extendhfsf2.c
80  ffsdi2.c
81  ffssi2.c
82  ffsti2.c
83  fixdfdi.c
84  fixdfsi.c
85  fixdfti.c
86  fixsfdi.c
87  fixsfsi.c
88  fixsfti.c
89  fixunsdfdi.c
90  fixunsdfsi.c
91  fixunsdfti.c
92  fixunssfdi.c
93  fixunssfsi.c
94  fixunssfti.c
95  floatdidf.c
96  floatdisf.c
97  floatsidf.c
98  floatsisf.c
99  floattidf.c
100  floattisf.c
101  floatundidf.c
102  floatundisf.c
103  floatunsidf.c
104  floatunsisf.c
105  floatuntidf.c
106  floatuntisf.c
107  fp_mode.c
108  int_util.c
109  lshrdi3.c
110  lshrti3.c
111  moddi3.c
112  modsi3.c
113  modti3.c
114  muldc3.c
115  muldf3.c
116  muldi3.c
117  mulodi4.c
118  mulosi4.c
119  muloti4.c
120  mulsc3.c
121  mulsf3.c
122  multi3.c
123  mulvdi3.c
124  mulvsi3.c
125  mulvti3.c
126  negdf2.c
127  negdi2.c
128  negsf2.c
129  negti2.c
130  negvdi2.c
131  negvsi2.c
132  negvti2.c
133  os_version_check.c
134  paritydi2.c
135  paritysi2.c
136  parityti2.c
137  popcountdi2.c
138  popcountsi2.c
139  popcountti2.c
140  powidf2.c
141  powisf2.c
142  subdf3.c
143  subsf3.c
144  subvdi3.c
145  subvsi3.c
146  subvti3.c
147  trampoline_setup.c
148  truncdfhf2.c
149  truncdfsf2.c
150  truncsfhf2.c
151  ucmpdi2.c
152  ucmpti2.c
153  udivdi3.c
154  udivmoddi4.c
155  udivmodsi4.c
156  udivmodti4.c
157  udivsi3.c
158  udivti3.c
159  umoddi3.c
160  umodsi3.c
161  umodti3.c
162)
163
164set(GENERIC_TF_SOURCES
165  addtf3.c
166  comparetf2.c
167  divtc3.c
168  divtf3.c
169  extenddftf2.c
170  extendsftf2.c
171  fixtfdi.c
172  fixtfsi.c
173  fixtfti.c
174  fixunstfdi.c
175  fixunstfsi.c
176  fixunstfti.c
177  floatditf.c
178  floatsitf.c
179  floattitf.c
180  floatunditf.c
181  floatunsitf.c
182  floatuntitf.c
183  multc3.c
184  multf3.c
185  powitf2.c
186  subtf3.c
187  trunctfdf2.c
188  trunctfsf2.c
189)
190
191option(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN
192  "Skip the atomic builtin (these should normally be provided by a shared library)"
193  On)
194
195if(NOT FUCHSIA AND NOT COMPILER_RT_BAREMETAL_BUILD)
196  set(GENERIC_SOURCES
197    ${GENERIC_SOURCES}
198    emutls.c
199    enable_execute_stack.c
200    eprintf.c
201  )
202endif()
203
204if(COMPILER_RT_HAS_ATOMIC_KEYWORD AND NOT COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN)
205  set(GENERIC_SOURCES
206    ${GENERIC_SOURCES}
207    atomic.c
208  )
209endif()
210
211if(APPLE)
212  set(GENERIC_SOURCES
213    ${GENERIC_SOURCES}
214    atomic_flag_clear.c
215    atomic_flag_clear_explicit.c
216    atomic_flag_test_and_set.c
217    atomic_flag_test_and_set_explicit.c
218    atomic_signal_fence.c
219    atomic_thread_fence.c
220  )
221endif()
222
223if (HAVE_UNWIND_H)
224  set(GENERIC_SOURCES
225    ${GENERIC_SOURCES}
226    gcc_personality_v0.c
227  )
228endif ()
229
230if (NOT FUCHSIA)
231  set(GENERIC_SOURCES
232    ${GENERIC_SOURCES}
233    clear_cache.c
234  )
235endif()
236
237# These sources work on all x86 variants, but only x86 variants.
238set(x86_ARCH_SOURCES
239  cpu_model.c
240  divxc3.c
241  fixxfdi.c
242  fixxfti.c
243  fixunsxfdi.c
244  fixunsxfsi.c
245  fixunsxfti.c
246  floatdixf.c
247  floattixf.c
248  floatundixf.c
249  floatuntixf.c
250  mulxc3.c
251  powixf2.c
252)
253
254if (NOT MSVC)
255  set(x86_ARCH_SOURCES
256    ${x86_ARCH_SOURCES}
257    i386/fp_mode.c
258  )
259endif ()
260
261if (NOT MSVC)
262  set(x86_64_SOURCES
263    ${GENERIC_SOURCES}
264    ${GENERIC_TF_SOURCES}
265    ${x86_ARCH_SOURCES}
266    x86_64/floatdidf.c
267    x86_64/floatdisf.c
268    x86_64/floatdixf.c
269    x86_64/floatundidf.S
270    x86_64/floatundisf.S
271    x86_64/floatundixf.S
272  )
273
274  # Darwin x86_64 Haswell
275  set(x86_64h_SOURCES ${x86_64_SOURCES})
276
277  if (WIN32)
278    set(x86_64_SOURCES
279      ${x86_64_SOURCES}
280      x86_64/chkstk.S
281      x86_64/chkstk2.S
282    )
283  endif()
284
285  set(i386_SOURCES
286    ${GENERIC_SOURCES}
287    ${x86_ARCH_SOURCES}
288    i386/ashldi3.S
289    i386/ashrdi3.S
290    i386/divdi3.S
291    i386/floatdidf.S
292    i386/floatdisf.S
293    i386/floatdixf.S
294    i386/floatundidf.S
295    i386/floatundisf.S
296    i386/floatundixf.S
297    i386/lshrdi3.S
298    i386/moddi3.S
299    i386/muldi3.S
300    i386/udivdi3.S
301    i386/umoddi3.S
302  )
303
304  if (WIN32)
305    set(i386_SOURCES
306      ${i386_SOURCES}
307      i386/chkstk.S
308      i386/chkstk2.S
309    )
310  endif()
311else () # MSVC
312  # Use C versions of functions when building on MSVC
313  # MSVC's assembler takes Intel syntax, not AT&T syntax.
314  # Also use only MSVC compilable builtin implementations.
315  set(x86_64_SOURCES
316    ${GENERIC_SOURCES}
317    ${x86_ARCH_SOURCES}
318    x86_64/floatdidf.c
319    x86_64/floatdisf.c
320    x86_64/floatdixf.c
321  )
322  set(i386_SOURCES ${GENERIC_SOURCES} ${x86_ARCH_SOURCES})
323endif () # if (NOT MSVC)
324
325set(arm_SOURCES
326  arm/fp_mode.c
327  arm/bswapdi2.S
328  arm/bswapsi2.S
329  arm/clzdi2.S
330  arm/clzsi2.S
331  arm/comparesf2.S
332  arm/divmodsi4.S
333  arm/divsi3.S
334  arm/modsi3.S
335  arm/sync_fetch_and_add_4.S
336  arm/sync_fetch_and_add_8.S
337  arm/sync_fetch_and_and_4.S
338  arm/sync_fetch_and_and_8.S
339  arm/sync_fetch_and_max_4.S
340  arm/sync_fetch_and_max_8.S
341  arm/sync_fetch_and_min_4.S
342  arm/sync_fetch_and_min_8.S
343  arm/sync_fetch_and_nand_4.S
344  arm/sync_fetch_and_nand_8.S
345  arm/sync_fetch_and_or_4.S
346  arm/sync_fetch_and_or_8.S
347  arm/sync_fetch_and_sub_4.S
348  arm/sync_fetch_and_sub_8.S
349  arm/sync_fetch_and_umax_4.S
350  arm/sync_fetch_and_umax_8.S
351  arm/sync_fetch_and_umin_4.S
352  arm/sync_fetch_and_umin_8.S
353  arm/sync_fetch_and_xor_4.S
354  arm/sync_fetch_and_xor_8.S
355  arm/udivmodsi4.S
356  arm/udivsi3.S
357  arm/umodsi3.S
358  ${GENERIC_SOURCES}
359)
360
361set(thumb1_SOURCES
362  arm/divsi3.S
363  arm/udivsi3.S
364  arm/comparesf2.S
365  arm/addsf3.S
366  ${GENERIC_SOURCES}
367)
368
369set(arm_EABI_SOURCES
370  arm/aeabi_cdcmp.S
371  arm/aeabi_cdcmpeq_check_nan.c
372  arm/aeabi_cfcmp.S
373  arm/aeabi_cfcmpeq_check_nan.c
374  arm/aeabi_dcmp.S
375  arm/aeabi_div0.c
376  arm/aeabi_drsub.c
377  arm/aeabi_fcmp.S
378  arm/aeabi_frsub.c
379  arm/aeabi_idivmod.S
380  arm/aeabi_ldivmod.S
381  arm/aeabi_memcmp.S
382  arm/aeabi_memcpy.S
383  arm/aeabi_memmove.S
384  arm/aeabi_memset.S
385  arm/aeabi_uidivmod.S
386  arm/aeabi_uldivmod.S
387)
388
389set(arm_Thumb1_JT_SOURCES
390  arm/switch16.S
391  arm/switch32.S
392  arm/switch8.S
393  arm/switchu8.S
394)
395set(arm_Thumb1_SjLj_EH_SOURCES
396  arm/restore_vfp_d8_d15_regs.S
397  arm/save_vfp_d8_d15_regs.S
398)
399set(arm_Thumb1_VFPv2_SOURCES
400  arm/adddf3vfp.S
401  arm/addsf3vfp.S
402  arm/divdf3vfp.S
403  arm/divsf3vfp.S
404  arm/eqdf2vfp.S
405  arm/eqsf2vfp.S
406  arm/extendsfdf2vfp.S
407  arm/fixdfsivfp.S
408  arm/fixsfsivfp.S
409  arm/fixunsdfsivfp.S
410  arm/fixunssfsivfp.S
411  arm/floatsidfvfp.S
412  arm/floatsisfvfp.S
413  arm/floatunssidfvfp.S
414  arm/floatunssisfvfp.S
415  arm/gedf2vfp.S
416  arm/gesf2vfp.S
417  arm/gtdf2vfp.S
418  arm/gtsf2vfp.S
419  arm/ledf2vfp.S
420  arm/lesf2vfp.S
421  arm/ltdf2vfp.S
422  arm/ltsf2vfp.S
423  arm/muldf3vfp.S
424  arm/mulsf3vfp.S
425  arm/nedf2vfp.S
426  arm/negdf2vfp.S
427  arm/negsf2vfp.S
428  arm/nesf2vfp.S
429  arm/subdf3vfp.S
430  arm/subsf3vfp.S
431  arm/truncdfsf2vfp.S
432  arm/unorddf2vfp.S
433  arm/unordsf2vfp.S
434)
435set(arm_Thumb1_icache_SOURCES
436  arm/sync_synchronize.S
437)
438set(arm_Thumb1_SOURCES
439  ${arm_Thumb1_JT_SOURCES}
440  ${arm_Thumb1_SjLj_EH_SOURCES}
441  ${arm_Thumb1_VFPv2_SOURCES}
442  ${arm_Thumb1_icache_SOURCES}
443)
444
445if(MINGW)
446  set(arm_SOURCES
447    arm/aeabi_idivmod.S
448    arm/aeabi_ldivmod.S
449    arm/aeabi_uidivmod.S
450    arm/aeabi_uldivmod.S
451    arm/chkstk.S
452    mingw_fixfloat.c
453    ${GENERIC_SOURCES}
454  )
455elseif(NOT WIN32)
456  # TODO the EABI sources should only be added to EABI targets
457  set(arm_SOURCES
458    ${arm_SOURCES}
459    ${arm_EABI_SOURCES}
460    ${arm_Thumb1_SOURCES}
461  )
462
463  set(thumb1_SOURCES
464    ${thumb1_SOURCES}
465    ${arm_EABI_SOURCES}
466  )
467endif()
468
469set(aarch64_SOURCES
470  ${GENERIC_TF_SOURCES}
471  ${GENERIC_SOURCES}
472  aarch64/fp_mode.c
473)
474
475if (MINGW)
476  set(aarch64_SOURCES
477    ${aarch64_SOURCES}
478    aarch64/chkstk.S
479  )
480endif()
481
482set(armhf_SOURCES ${arm_SOURCES})
483set(armv7_SOURCES ${arm_SOURCES})
484set(armv7s_SOURCES ${arm_SOURCES})
485set(armv7k_SOURCES ${arm_SOURCES})
486set(arm64_SOURCES ${aarch64_SOURCES})
487set(arm64e_SOURCES ${aarch64_SOURCES})
488
489# macho_embedded archs
490set(armv6m_SOURCES ${thumb1_SOURCES})
491set(armv7m_SOURCES ${arm_SOURCES})
492set(armv7em_SOURCES ${arm_SOURCES})
493
494# hexagon arch
495set(hexagon_SOURCES
496  hexagon/common_entry_exit_abi1.S
497  hexagon/common_entry_exit_abi2.S
498  hexagon/common_entry_exit_legacy.S
499  hexagon/dfaddsub.S
500  hexagon/dfdiv.S
501  hexagon/dffma.S
502  hexagon/dfminmax.S
503  hexagon/dfmul.S
504  hexagon/dfsqrt.S
505  hexagon/divdi3.S
506  hexagon/divsi3.S
507  hexagon/fastmath2_dlib_asm.S
508  hexagon/fastmath2_ldlib_asm.S
509  hexagon/fastmath_dlib_asm.S
510  hexagon/memcpy_forward_vp4cp4n2.S
511  hexagon/memcpy_likely_aligned.S
512  hexagon/moddi3.S
513  hexagon/modsi3.S
514  hexagon/sfdiv_opt.S
515  hexagon/sfsqrt_opt.S
516  hexagon/udivdi3.S
517  hexagon/udivmoddi4.S
518  hexagon/udivmodsi4.S
519  hexagon/udivsi3.S
520  hexagon/umoddi3.S
521  hexagon/umodsi3.S
522  ${GENERIC_SOURCES}
523  ${GENERIC_TF_SOURCES}
524)
525
526
527set(mips_SOURCES ${GENERIC_SOURCES})
528set(mipsel_SOURCES ${mips_SOURCES})
529set(mips64_SOURCES ${GENERIC_TF_SOURCES}
530                   ${mips_SOURCES})
531set(mips64el_SOURCES ${GENERIC_TF_SOURCES}
532                     ${mips_SOURCES})
533
534set(powerpc64_SOURCES
535  ppc/divtc3.c
536  ppc/fixtfti.c
537  ppc/fixtfdi.c
538  ppc/fixunstfti.c
539  ppc/fixunstfdi.c
540  ppc/floattitf.c
541  ppc/floatditf.c
542  ppc/floatunditf.c
543  ppc/gcc_qadd.c
544  ppc/gcc_qdiv.c
545  ppc/gcc_qmul.c
546  ppc/gcc_qsub.c
547  ppc/multc3.c
548  ${GENERIC_SOURCES}
549)
550set(powerpc64le_SOURCES ${powerpc64_SOURCES})
551
552set(riscv_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})
553set(riscv32_SOURCES
554  riscv/mulsi3.S
555  ${riscv_SOURCES}
556)
557set(riscv64_SOURCES ${riscv_SOURCES})
558
559set(sparc_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})
560set(sparcv9_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})
561
562set(wasm32_SOURCES
563  ${GENERIC_TF_SOURCES}
564  ${GENERIC_SOURCES}
565)
566set(wasm64_SOURCES
567  ${GENERIC_TF_SOURCES}
568  ${GENERIC_SOURCES}
569)
570
571set(ve_SOURCES
572  ve/grow_stack.S
573  ve/grow_stack_align.S
574  ${GENERIC_TF_SOURCES}
575  ${GENERIC_SOURCES})
576
577add_custom_target(builtins)
578set_target_properties(builtins PROPERTIES FOLDER "Compiler-RT Misc")
579
580if (APPLE)
581  add_subdirectory(Darwin-excludes)
582  add_subdirectory(macho_embedded)
583  darwin_add_builtin_libraries(${BUILTIN_SUPPORTED_OS})
584else ()
585  set(BUILTIN_CFLAGS "")
586
587  append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 BUILTIN_CFLAGS)
588
589  # These flags would normally be added to CMAKE_C_FLAGS by the llvm
590  # cmake step. Add them manually if this is a standalone build.
591  if(COMPILER_RT_STANDALONE_BUILD)
592    if(COMPILER_RT_BUILTINS_ENABLE_PIC)
593      append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC BUILTIN_CFLAGS)
594    endif()
595    append_list_if(COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin BUILTIN_CFLAGS)
596    if(NOT ANDROID)
597      append_list_if(COMPILER_RT_HAS_VISIBILITY_HIDDEN_FLAG -fvisibility=hidden BUILTIN_CFLAGS)
598    endif()
599    if(NOT COMPILER_RT_DEBUG)
600      append_list_if(COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG -fomit-frame-pointer BUILTIN_CFLAGS)
601    endif()
602  endif()
603
604  set(BUILTIN_DEFS "")
605
606  if(NOT ANDROID)
607    append_list_if(COMPILER_RT_HAS_VISIBILITY_HIDDEN_FLAG VISIBILITY_HIDDEN BUILTIN_DEFS)
608  endif()
609
610  foreach (arch ${BUILTIN_SUPPORTED_ARCH})
611    if (CAN_TARGET_${arch})
612      # For ARM archs, exclude any VFP builtins if VFP is not supported
613      if (${arch} MATCHES "^(arm|armhf|armv7|armv7s|armv7k|armv7m|armv7em)$")
614        string(REPLACE ";" " " _TARGET_${arch}_CFLAGS "${TARGET_${arch}_CFLAGS}")
615        check_compile_definition(__VFP_FP__ "${CMAKE_C_FLAGS} ${_TARGET_${arch}_CFLAGS}" COMPILER_RT_HAS_${arch}_VFP)
616        if(NOT COMPILER_RT_HAS_${arch}_VFP)
617          list(REMOVE_ITEM ${arch}_SOURCES ${arm_Thumb1_VFPv2_SOURCES} ${arm_Thumb1_SjLj_EH_SOURCES})
618        endif()
619      endif()
620
621      # Remove a generic C builtin when an arch-specific builtin is specified.
622      filter_builtin_sources(${arch}_SOURCES ${arch})
623
624      # Needed for clear_cache on debug mode, due to r7's usage in inline asm.
625      # Release mode already sets it via -O2/3, Debug mode doesn't.
626      if (${arch} STREQUAL "armhf")
627        list(APPEND BUILTIN_CFLAGS -fomit-frame-pointer -DCOMPILER_RT_ARMHF_TARGET)
628      endif()
629
630      # For RISCV32, we must force enable int128 for compiling long
631      # double routines.
632      if("${arch}" STREQUAL "riscv32")
633        list(APPEND BUILTIN_CFLAGS -fforce-enable-int128)
634      endif()
635
636      add_compiler_rt_runtime(clang_rt.builtins
637                              STATIC
638                              ARCHS ${arch}
639                              SOURCES ${${arch}_SOURCES}
640                              DEFS ${BUILTIN_DEFS}
641                              CFLAGS ${BUILTIN_CFLAGS}
642                              PARENT_TARGET builtins)
643    endif ()
644  endforeach ()
645endif ()
646
647add_dependencies(compiler-rt builtins)
648