xref: /llvm-project/compiler-rt/lib/profile/CMakeLists.txt (revision d4efc3e097f40afbe8ae275150f49bb08fc04572)
1
2CHECK_CXX_SOURCE_COMPILES("
3#ifdef _WIN32
4#include <intrin.h> /* Workaround for PR19898. */
5#include <windows.h>
6#endif
7int main() {
8#ifdef _WIN32
9        volatile LONG val = 1;
10        MemoryBarrier();
11        InterlockedCompareExchange(&val, 0, 1);
12        InterlockedIncrement(&val);
13        InterlockedDecrement(&val);
14#else
15        volatile unsigned long val = 1;
16        __sync_synchronize();
17        __sync_val_compare_and_swap(&val, 1, 0);
18        __sync_add_and_fetch(&val, 1);
19        __sync_sub_and_fetch(&val, 1);
20#endif
21        return 0;
22      }
23" COMPILER_RT_TARGET_HAS_ATOMICS)
24
25CHECK_CXX_SOURCE_COMPILES("
26#if defined(__linux__)
27#include <unistd.h>
28#endif
29#include <fcntl.h>
30int fd;
31int main() {
32 struct flock s_flock;
33
34 s_flock.l_type = F_WRLCK;
35 fcntl(fd, F_SETLKW, &s_flock);
36 return 0;
37}
38
39" COMPILER_RT_TARGET_HAS_FCNTL_LCK)
40
41CHECK_CXX_SOURCE_COMPILES("
42#include <sys/file.h>
43
44int fd;
45int main() {
46  flock(fd, LOCK_EX);
47  return 0;
48}
49
50" COMPILER_RT_TARGET_HAS_FLOCK)
51
52CHECK_CXX_SOURCE_COMPILES("
53#include <sys/utsname.h>
54int main() {
55 return 0;
56}
57
58" COMPILER_RT_TARGET_HAS_UNAME)
59
60add_compiler_rt_component(profile)
61
62set(PROFILE_SOURCES
63  GCDAProfiling.c
64  InstrProfiling.c
65  InstrProfilingInternal.c
66  InstrProfilingValue.c
67  InstrProfilingBuffer.c
68  InstrProfilingFile.c
69  InstrProfilingMerge.c
70  InstrProfilingMergeFile.c
71  InstrProfilingNameVar.c
72  InstrProfilingVersionVar.c
73  InstrProfilingWriter.c
74  InstrProfilingPlatformAIX.c
75  InstrProfilingPlatformDarwin.c
76  InstrProfilingPlatformFuchsia.c
77  InstrProfilingPlatformLinux.c
78  InstrProfilingPlatformOther.c
79  InstrProfilingPlatformWindows.c
80  InstrProfilingRuntime.cpp
81  InstrProfilingUtil.c
82  )
83
84set(PROFILE_HEADERS
85  InstrProfiling.h
86  InstrProfilingInternal.h
87  InstrProfilingPort.h
88  InstrProfilingUtil.h
89  WindowsMMap.h
90  )
91
92if(WIN32)
93  list(APPEND PROFILE_SOURCES
94    WindowsMMap.c
95  )
96endif()
97
98include_directories(..)
99include_directories(../../include)
100
101if(FUCHSIA OR UNIX)
102 set(EXTRA_FLAGS
103     -fPIC
104     -Wno-pedantic)
105endif()
106
107if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
108  set(EXTRA_FLAGS
109      ${EXTRA_FLAGS}
110      -D_WASI_EMULATED_MMAN
111      -D_WASI_EMULATED_GETPID)
112endif()
113
114if(COMPILER_RT_TARGET_HAS_ATOMICS)
115 set(EXTRA_FLAGS
116     ${EXTRA_FLAGS}
117     -DCOMPILER_RT_HAS_ATOMICS=1)
118endif()
119
120if(COMPILER_RT_TARGET_HAS_FCNTL_LCK)
121 set(EXTRA_FLAGS
122     ${EXTRA_FLAGS}
123     -DCOMPILER_RT_HAS_FCNTL_LCK=1)
124endif()
125
126if(COMPILER_RT_TARGET_HAS_FLOCK)
127  set(EXTRA_FLAGS
128      ${EXTRA_FLAGS}
129      -DCOMPILER_RT_HAS_FLOCK=1)
130endif()
131
132if(COMPILER_RT_TARGET_HAS_UNAME)
133 set(EXTRA_FLAGS
134     ${EXTRA_FLAGS}
135     -DCOMPILER_RT_HAS_UNAME=1)
136endif()
137
138if(MSVC)
139  # profile historically has only been supported with the static runtime
140  # on windows
141  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
142endif()
143
144# We don't use the C++ Standard Library here, so avoid including it by mistake.
145append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ EXTRA_FLAGS)
146# XRay uses C++ standard library headers.
147string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
148
149# This appears to be a C-only warning banning the use of locals in aggregate
150# initializers. All other compilers accept this, though.
151# nonstandard extension used : 'identifier' : cannot be initialized using address of automatic variable
152append_list_if(COMPILER_RT_HAS_WD4221_FLAG /wd4221 EXTRA_FLAGS)
153
154# Disable 'nonstandard extension used: translation unit is empty'.
155append_list_if(COMPILER_RT_HAS_WD4206_FLAG /wd4206 EXTRA_FLAGS)
156
157if(APPLE)
158  add_compiler_rt_runtime(clang_rt.profile
159    STATIC
160    OS ${PROFILE_SUPPORTED_OS}
161    ARCHS ${PROFILE_SUPPORTED_ARCH}
162    CFLAGS ${EXTRA_FLAGS}
163    SOURCES ${PROFILE_SOURCES}
164    ADDITIONAL_HEADERS ${PROFILE_HEADERS}
165    PARENT_TARGET profile
166    EXTENSIONS ON)
167else()
168  add_compiler_rt_runtime(clang_rt.profile
169    STATIC
170    ARCHS ${PROFILE_SUPPORTED_ARCH}
171    CFLAGS ${EXTRA_FLAGS}
172    SOURCES ${PROFILE_SOURCES}
173    ADDITIONAL_HEADERS ${PROFILE_HEADERS}
174    PARENT_TARGET profile
175    EXTENSIONS ON)
176endif()
177