Stop treating -static as overriding -fPIC: they are distinct.
[lldb.git] / llgo / CMakeLists.txt
1 include(ExternalProject)
2 include(ProcessorCount)
3
4 # Provide a config.h which exposes build system information.
5 configure_file(
6   cmd/gllgo/config.h.cmake
7   ${CMAKE_CURRENT_BINARY_DIR}/cmd/gllgo/config.h)
8 include_directories(${CMAKE_CURRENT_BINARY_DIR}/cmd/gllgo)
9
10 llvm_add_go_executable(llgo llvm.org/llgo/cmd/gllgo ALL DEPENDS
11   build/context.go
12   cmd/gllgo/gllgo.go
13   debug/debug.go
14   driver/parser.go
15   irgen/annotations.go
16   irgen/attribute.go
17   irgen/builtins.go
18   irgen/cabi.go
19   irgen/call.go
20   irgen/channels.go
21   irgen/closures.go
22   irgen/compiler.go
23   irgen/errors.go
24   irgen/indirect.go
25   irgen/interfaces.go
26   irgen/maps.go
27   irgen/predicates.go
28   irgen/println.go
29   irgen/runtime.go
30   irgen/slice.go
31   irgen/ssa.go
32   irgen/strings.go
33   irgen/switches.go
34   irgen/targets.go
35   irgen/typemap.go
36   irgen/types.go
37   irgen/utils.go
38   irgen/value.go
39   irgen/version.go
40   ssaopt/esc.go
41 )
42
43 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" LLGO_VERSION
44   ${PACKAGE_VERSION})
45
46 configure_file(
47   cmd/go/zdefaultcc.go.in
48   ${CMAKE_CURRENT_BINARY_DIR}/cmd/go/zdefaultcc.go)
49
50 set(LLGO_GO_SOURCES
51   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/build.go
52   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/clean.go
53   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/context.go
54   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/discovery.go
55   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/doc.go
56   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/env.go
57   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/fix.go
58   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/fmt.go
59   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/generate.go
60   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/get.go
61   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/go11.go
62   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/help.go
63   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/http.go
64   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/list.go
65   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/main.go
66   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/pkg.go
67   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/run.go
68   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/signal.go
69   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/signal_unix.go
70   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/testflag.go
71   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/test.go
72   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/tool.go
73   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/vcs.go
74   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/version.go
75   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/go/vet.go
76   ${CMAKE_CURRENT_BINARY_DIR}/cmd/go/zdefaultcc.go
77 )
78
79 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/bin/llgo-go${CMAKE_EXECUTABLE_SUFFIX}
80   COMMAND ${CMAKE_BINARY_DIR}/bin/llgo -static-libgo
81           -o ${CMAKE_BINARY_DIR}/bin/llgo-go${CMAKE_EXECUTABLE_SUFFIX}
82           ${LLGO_GO_SOURCES}
83           ${CMAKE_CURRENT_BINARY_DIR}/libgo/zstdpkglist.go
84   DEPENDS llgo libgo ${LLGO_GO_SOURCES}
85   COMMENT "Building Go executable llgo-go"
86   VERBATIM)
87 add_custom_target(llgo-go ALL DEPENDS ${CMAKE_BINARY_DIR}/bin/llgo-go${CMAKE_EXECUTABLE_SUFFIX})
88
89 set(LLGO_CGO_SOURCES
90   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/cgo/ast.go
91   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/cgo/doc.go
92   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/cgo/gcc.go
93   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/cgo/godefs.go
94   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/cgo/main.go
95   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/cgo/out.go
96   ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo/go/cmd/cgo/util.go
97   ${CMAKE_CURRENT_SOURCE_DIR}/cmd/cgo/zdefaultcc.go
98 )
99
100 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/lib/go/llgo-${LLGO_VERSION}/cgo
101   COMMAND ${CMAKE_BINARY_DIR}/bin/llgo -static-libgo
102           -o ${CMAKE_BINARY_DIR}/lib/go/llgo-${LLGO_VERSION}/cgo
103           ${LLGO_CGO_SOURCES}
104   DEPENDS llgo libgo ${LLGO_CGO_SOURCES}
105   COMMENT "Building Go executable cgo"
106   VERBATIM)
107 add_custom_target(cgo ALL DEPENDS ${CMAKE_BINARY_DIR}/lib/go/llgo-${LLGO_VERSION}/cgo)
108
109 llvm_add_go_executable(llgo-stage2 llvm.org/llgo/cmd/gllgo
110   DEPENDS libgo ${CMAKE_BINARY_DIR}/bin/llgo${CMAKE_EXECUTABLE_SUFFIX}
111           ${CMAKE_BINARY_DIR}/lib/go/llgo-${LLGO_VERSION}/cgo
112           ${CMAKE_BINARY_DIR}/bin/llgo-go${CMAKE_EXECUTABLE_SUFFIX}
113   GOFLAGS "cc=${CMAKE_BINARY_DIR}/bin/clang"
114           "cxx=${CMAKE_BINARY_DIR}/bin/clang++"
115           "go=${CMAKE_BINARY_DIR}/bin/llgo-go"
116 )
117
118 llvm_add_go_executable(llgo-stage3 llvm.org/llgo/cmd/gllgo
119   DEPENDS libgo ${CMAKE_BINARY_DIR}/bin/llgo-stage2${CMAKE_EXECUTABLE_SUFFIX}
120   GOFLAGS "cc=${CMAKE_BINARY_DIR}/bin/clang"
121           "cxx=${CMAKE_BINARY_DIR}/bin/clang++"
122           "go=${CMAKE_BINARY_DIR}/bin/llgo-go"
123           "llgo=${CMAKE_BINARY_DIR}/bin/llgo-stage2${CMAKE_EXECUTABLE_SUFFIX}"
124 )
125
126 llvm_add_go_executable(cc-wrapper llvm.org/llgo/cmd/cc-wrapper DEPENDS
127   cmd/cc-wrapper/main.go
128 )
129
130 llvm_add_go_executable(llgoi llvm.org/llgo/cmd/llgoi ALL
131   DEPENDS libgo ${CMAKE_BINARY_DIR}/bin/llgo${CMAKE_EXECUTABLE_SUFFIX}
132           ${CMAKE_BINARY_DIR}/lib/go/llgo-${LLGO_VERSION}/cgo
133           ${CMAKE_BINARY_DIR}/bin/llgo-go${CMAKE_EXECUTABLE_SUFFIX}
134           cmd/llgoi/llgoi.go
135   GOFLAGS "cc=${CMAKE_BINARY_DIR}/bin/clang"
136           "cxx=${CMAKE_BINARY_DIR}/bin/clang++"
137           "go=${CMAKE_BINARY_DIR}/bin/llgo-go"
138 )
139
140 install(FILES ${CMAKE_BINARY_DIR}/bin/llgo${CMAKE_EXECUTABLE_SUFFIX}
141               ${CMAKE_BINARY_DIR}/bin/llgoi${CMAKE_EXECUTABLE_SUFFIX}
142               ${CMAKE_BINARY_DIR}/bin/llgo-go${CMAKE_EXECUTABLE_SUFFIX}
143         DESTINATION bin
144         PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
145                     GROUP_READ GROUP_EXECUTE
146                     WORLD_READ WORLD_EXECUTE)
147
148 function(add_clobber_steps name)
149   ExternalProject_Add_Step(${name} force-reconfigure
150     DEPENDERS configure
151     ALWAYS 1
152     )
153
154   ExternalProject_Add_Step(${name} clobber
155     COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
156     COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
157     COMMENT "Clobbering ${name} build directory..."
158     DEPENDERS configure
159     DEPENDS ${ARGN}
160     )
161 endfunction()
162
163 processorcount(PROCESSOR_COUNT)
164
165 function(add_libgo_variant suffix cflags gocflags deps exclude_from_all)
166   externalproject_add(libbacktrace${suffix}
167     DEPENDS clang ${deps}
168     SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libbacktrace
169     BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${suffix}/libbacktrace
170     CONFIGURE_COMMAND <SOURCE_DIR>/configure --disable-multilib --enable-host-shared "CC=${CMAKE_BINARY_DIR}/bin/clang ${cflags}"
171     BUILD_COMMAND make -j${PROCESSOR_COUNT}
172     INSTALL_COMMAND ""
173     LOG_CONFIGURE 1
174     LOG_BUILD 1
175   )
176   set_property(TARGET libbacktrace${suffix}
177                PROPERTY EXCLUDE_FROM_ALL ${exclude_from_all})
178
179   add_clobber_steps(libbacktrace${suffix} clang ${deps})
180
181   externalproject_add(libffi${suffix}
182     DEPENDS clang ${deps}
183     SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libffi
184     BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${suffix}/libffi
185     CONFIGURE_COMMAND <SOURCE_DIR>/configure --disable-multilib "CC=${CMAKE_BINARY_DIR}/bin/clang ${cflags}"
186     BUILD_COMMAND make -j${PROCESSOR_COUNT}
187     INSTALL_COMMAND ""
188     LOG_CONFIGURE 1
189     LOG_BUILD 1
190   )
191   set_property(TARGET libffi${suffix}
192                PROPERTY EXCLUDE_FROM_ALL ${exclude_from_all})
193
194   add_clobber_steps(libffi${suffix} clang ${deps})
195
196   externalproject_add(libgo${suffix}
197     DEPENDS clang llgo cc-wrapper libbacktrace${suffix} libffi${suffix} ${deps}
198     SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gofrontend/libgo
199     BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${suffix}/libgo
200     INSTALL_DIR ${CMAKE_BINARY_DIR}
201     CONFIGURE_COMMAND <SOURCE_DIR>/configure --disable-multilib --without-libatomic --prefix=<INSTALL_DIR> "CC=env REAL_CC=${CMAKE_BINARY_DIR}/bin/clang@SPACE@${cflags} ${CMAKE_BINARY_DIR}/bin/cc-wrapper" "GOC=${CMAKE_BINARY_DIR}/bin/llgo -no-prefix -fcompilerrt-prefix=${CMAKE_BINARY_DIR} ${gocflags}"
202     BUILD_COMMAND make -j${PROCESSOR_COUNT}
203     LOG_CONFIGURE 1
204     LOG_BUILD 1
205     LOG_INSTALL 1
206   )
207   set_property(TARGET libgo${suffix}
208                PROPERTY EXCLUDE_FROM_ALL ${exclude_from_all})
209
210   add_clobber_steps(libgo${suffix} clang
211     ${CMAKE_BINARY_DIR}/bin/llgo${CMAKE_EXECUTABLE_SUFFIX}
212     ${CMAKE_BINARY_DIR}/bin/cc-wrapper${CMAKE_EXECUTABLE_SUFFIX})
213 endfunction()
214
215 add_libgo_variant("" "" "" "" FALSE)
216
217 if(TARGET asan)
218   add_libgo_variant("_asan" "-fsanitize=address" "-fsanitize=address" asan TRUE)
219 endif()
220
221 if(TARGET tsan)
222   add_libgo_variant("_tsan" "-fsanitize=thread" "-fsanitize=thread" tsan TRUE)
223 endif()
224
225 if(TARGET msan)
226   add_libgo_variant("_msan" "-fsanitize=memory" "-fsanitize=memory" msan TRUE)
227 endif()
228
229 if(TARGET dfsan)
230   add_libgo_variant("_dfsan" "-fsanitize=dataflow" "-fsanitize=dataflow" dfsan TRUE)
231 endif()
232
233 set(LLGO_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
234
235 install(FILES ${LLGO_LIBRARY_DIR}/libgo-llgo.a
236               ${LLGO_LIBRARY_DIR}/libgo-llgo.so
237               ${LLGO_LIBRARY_DIR}/libgo-llgo.so.7
238               ${LLGO_LIBRARY_DIR}/libgo-llgo.so.7.0.0
239               ${LLGO_LIBRARY_DIR}/libgobegin-llgo.a
240         DESTINATION lib${LLVM_LIBDIR_SUFFIX})
241
242 install(DIRECTORY ${LLGO_LIBRARY_DIR}/go
243         DESTINATION lib${LLVM_LIBDIR_SUFFIX})
244
245 add_custom_target(check-libgo
246   COMMAND make -C ${CMAKE_CURRENT_BINARY_DIR}/libgo -j${PROCESSOR_COUNT} check
247   DEPENDS libgo
248   COMMENT "Running libgo tests")
249
250 add_custom_target(check-llgo-bootstrap
251   COMMAND strip -R .note.gnu.build-id -o ${CMAKE_CURRENT_BINARY_DIR}/llgo-stage2.stripped
252           ${CMAKE_BINARY_DIR}/bin/llgo-stage2${CMAKE_EXECUTABLE_SUFFIX}
253   COMMAND strip -R .note.gnu.build-id -o ${CMAKE_CURRENT_BINARY_DIR}/llgo-stage3.stripped
254           ${CMAKE_BINARY_DIR}/bin/llgo-stage3${CMAKE_EXECUTABLE_SUFFIX}
255   COMMAND cmp ${CMAKE_CURRENT_BINARY_DIR}/llgo-stage2.stripped
256           ${CMAKE_CURRENT_BINARY_DIR}/llgo-stage3.stripped
257   DEPENDS llgo-stage2 llgo-stage3
258   COMMENT "Checking llgo bootstrap")
259
260 add_subdirectory(test)