-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
1569 lines (1267 loc) · 56.7 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#=============================================================================
# CMake build system files
#
# Copyright (c) 2014-2017 pocl developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#=============================================================================
cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
project(pocl)
# Fix behavior of CMAKE_CXX_STANDARD when targeting macOS.
if(POLICY CMP0025)
cmake_policy(SET CMP0025 NEW)
endif()
# don't allow implicit function declarations
if(UNIX)
if((CMAKE_C_COMPILER_ID STREQUAL "GNU") OR
(CMAKE_C_COMPILER_ID STREQUAL "Clang"))
set(FORBIT_IMPLICIT_FUNCTIONS "-Werror=implicit-function-declaration")
add_compile_options("${FORBIT_IMPLICIT_FUNCTIONS}")
else()
message(WARNING "Don't know how to forbid this compiler from allowing implicit function declarations.")
endif()
endif()
set(MAJOR_VERSION 1)
set(MINOR_VERSION 3)
set(VERSION_SUFFIX "-pre")
set(VERSION_STRING ${MAJOR_VERSION}.${MINOR_VERSION}${VERSION_SUFFIX})
set(POCL_VERSION ${VERSION_STRING})
# required b/c SHARED libs defaults to ON while OBJECT defaults to OFF
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
enable_testing()
#####################################################
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
set(DEFAULT_BUILD_TYPE "Debug")
else()
set(DEFAULT_BUILD_TYPE "RelWithDebInfo")
endif()
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified.")
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}" CACHE
STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()
##################################################################################
macro(set_expr VAR)
if(${ARGN})
set(${VAR} 1)
else()
set(${VAR} 0)
endif()
endmacro()
find_program(BASH "bash")
find_program(MAKE_PROGRAM NAMES "make")
find_program(GIT_CMD "git")
set_expr(HAVE_GIT GIT_CMD)
if(HAVE_GIT AND (VERSION_SUFFIX MATCHES "pre"))
execute_process(COMMAND "${GIT_CMD}" "rev-parse" "HEAD"
OUTPUT_VARIABLE GIT_COMMIT
RESULT_VARIABLE EXITCODE
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "Pocl source Git commit: ${GIT_COMMIT}")
execute_process(COMMAND "${GIT_CMD}" "branch" "--contains" "${GIT_COMMIT}"
OUTPUT_VARIABLE GIT_BRANCH
RESULT_VARIABLE EXITCODE
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "Pocl source Git branch: ${GIT_BRANCH}")
execute_process(COMMAND "${GIT_CMD}" describe "--always" "--long" "--all" "${GIT_COMMIT}"
OUTPUT_VARIABLE GIT_DESCRIBE
RESULT_VARIABLE EXITCODE
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REPLACE "heads/" "" GIT_DESCRIBE "${GIT_DESCRIBE}")
message(STATUS "Pocl source Git describe: ${GIT_DESCRIBE}")
set(VERSION_SUFFIX "-pre/${GIT_DESCRIBE}")
set(VERSION_STRING ${MAJOR_VERSION}.${MINOR_VERSION}${VERSION_SUFFIX})
set(POCL_VERSION ${VERSION_STRING})
else()
message(STATUS "No git and/or not a prerelease -> not adding git commit to version.")
endif()
##################################################################################
option(OCS_AVAILABLE "Online compiler support available to build pocl with. Default is available." ON)
option(BUILD_SHARED_LIBS "ON=Build shared libs, OFF=static libs" ON)
option(POCL_DEBUG_MESSAGES
"Enable debug messages from pocl (useful for OpenCL developers), must be enabled at runtime, with env var POCL_DEBUG"
ON)
option(ENABLE_HSA "Enable the HSA device driver for AMD GCN devices" OFF)
option(ENABLE_CUDA "Enable the CUDA device driver for NVIDIA devices" OFF)
option(KERNEL_CACHE_DEFAULT "Default value for the kernel compile cache. If disabled, pocl will still use the kernel cache, but will delete cachefiles on exit. You can still enable keeping the files it at runtime with an env var." ON)
option(POCL_ICD_ABSOLUTE_PATH "Use absolute path in pocl.icd" ON)
option(ENABLE_POCL_BUILDING "When OFF, env var POCL_BUILDING has no effect. Defaults to ON" ON)
#### these are mostly useful for pocl developers
option(DEVELOPER_MODE "This will SIGNIFICANTLY slow down pocl (but speed up its compilation). Only turn on if you know what you're doing." OFF)
option(USE_POCL_MEMMANAGER "Enables custom memory manager. Except for special circumstances, this should be disabled." OFF)
option(EXAMPLES_USE_GIT_MASTER "If enabled, some of the external testsuites in examples/ will try to use sources from Git master, instead of releases. This may result in failure to build or run the examples" OFF)
####
# currently only works with gcc as host compiler
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99")
option(ENABLE_ASAN "Enable AddressSanitizer" OFF)
option(ENABLE_TSAN "Enable ThreadSanitizer" OFF)
else()
set(ENABLE_ASAN OFF CACHE INTERNAL "Enable AddressSanitizer")
set(ENABLE_TSAN OFF CACHE INTERNAL "Enable ThreadSanitizer")
endif()
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99")
option(ENABLE_UBSAN "Enable UBSanitizer" OFF)
else()
set(ENABLE_UBSAN OFF CACHE INTERNAL "Enable UBSanitizer")
endif()
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER "5.0.99")
option(ENABLE_LSAN "Enable LeakSanitizer" OFF)
else()
set(ENABLE_LSAN OFF CACHE INTERNAL "Enable LeakSanitizer")
endif()
else()
set(ENABLE_ASAN OFF CACHE INTERNAL "Enable AddressSanitizer")
set(ENABLE_TSAN OFF CACHE INTERNAL "Enable ThreadSanitizer")
endif()
##########################################################
# POCL_DEVICE_ADDRESS_BITS
# TODO rename to HOST address bits
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(POCL_DEVICE_ADDRESS_BITS 64)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(POCL_DEVICE_ADDRESS_BITS 32)
else()
message(FATAL_ERROR "Cannot figure out POCL_DEVICE_ADDRESS_BITS")
endif()
##################################################################################
if(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc")
set(POWERPC 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "mips")
set(MIPS 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "armv7")
set(ARMV7 1)
set(ARM32 1)
set(ARM 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "armv6")
set(ARMV6 1)
set(ARM32 1)
set(ARM 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
set(ARM64 1)
set(ARM 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(i.86|AMD64|x86_64|amd64)")
set(X86 1)
if(POCL_DEVICE_ADDRESS_BITS MATCHES "32")
set(I386 1)
else()
set(X86_64 1)
endif()
endif()
if(CMAKE_MAJOR_VERSION GREATER 2)
include(ProcessorCount)
ProcessorCount(CORECOUNT)
if(CORECOUNT LESS 1)
set(CORECOUNT 1)
endif()
else()
set(CORECOUNT 1)
endif()
message(STATUS "Host CPU cores: ${CORECOUNT}")
######################################################################################
function(rename_if_different SRC DST)
if(EXISTS "${DST}")
file(MD5 "${SRC}" OLD_MD5)
file(MD5 "${DST}" NEW_MD5)
if(NOT OLD_MD5 STREQUAL NEW_MD5)
file(RENAME "${SRC}" "${DST}")
endif()
else()
file(RENAME "${SRC}" "${DST}")
endif()
endfunction()
######################################################################################
# Recent versions of CMake can make use of Ninja's console pool to avoid
# buffering the output of particular commands.
if(CMAKE_VERSION VERSION_LESS 3.2.0)
set(COMMAND_USES_TERMINAL)
else()
set(COMMAND_USES_TERMINAL USES_TERMINAL)
endif()
if(UNIX)
include(GNUInstallDirs)
else()
if (WIN32)
set(${CMAKE_INSTALL_LIBDIR} "lib")
set(${CMAKE_INSTALL_DATADIR} "share")
set(${CMAKE_INSTALL_INCLUDEDIR} "include")
set(${CMAKE_INSTALL_BINDIR} "bin")
message(STATUS "Setting installation destination on Windows to: ${CMAKE_INSTALL_PREFIX}")
else()
message(FATAL_ERROR "System not UNIX nor WIN32 - not implemented yet")
endif()
endif()
# for libpocl.so
set(POCL_INSTALL_PUBLIC_LIBDIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" CACHE PATH "POCL public libdir")
# for llvmopencl.so
set(POCL_INSTALL_PRIVATE_LIBDIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pocl" CACHE PATH "POCL private libdir")
# for pocl.icd
if(UNIX AND (NOT CMAKE_CROSSCOMPILING) AND (CMAKE_INSTALL_PREFIX STREQUAL "/usr"))
set(POCL_INSTALL_ICD_VENDORDIR "/etc/OpenCL/vendors" CACHE PATH "POCL ICD file destination")
else()
set(POCL_INSTALL_ICD_VENDORDIR "${CMAKE_INSTALL_PREFIX}/etc/OpenCL/vendors" CACHE PATH "POCL ICD file destination")
endif()
# for kernel-<target>.bc
set(POCL_INSTALL_PRIVATE_DATADIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}/pocl" CACHE PATH "POCL private datadir")
# for poclu.h
set(POCL_INSTALL_PUBLIC_HEADER_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}" CACHE PATH "POCL public header dir")
# for _kernel.h et al
set(POCL_INSTALL_PRIVATE_HEADER_DIR "${POCL_INSTALL_PRIVATE_DATADIR}/include" CACHE PATH "POCL private header dir")
# for pocl-standalone et al
set(POCL_INSTALL_PUBLIC_BINDIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}" CACHE PATH "POCL public bindir")
# for PoclConfig.cmake & stuff
set(POCL_INSTALL_CMAKE_CONFIG_DIR "${POCL_INSTALL_PRIVATE_LIBDIR}" CACHE PATH "Installation directory for CMake files")
# TODO maybe use output of pkg-config --variable=pc_path pkg-config ?
set(POCL_INSTALL_PKGCONFIG_DIR "${POCL_INSTALL_PUBLIC_LIBDIR}/pkgconfig" CACHE PATH "Destination for pocl.pc")
if(APPLE)
set(CMAKE_MACOSX_RPATH ON)
set(POCL_INSTALL_OPENCL_HEADER_DIR "${POCL_INSTALL_PUBLIC_HEADER_DIR}/OpenCL" CACHE PATH "POCL header dir for OpenCL headers")
else()
set(POCL_INSTALL_OPENCL_HEADER_DIR "${POCL_INSTALL_PUBLIC_HEADER_DIR}/CL" CACHE PATH "POCL header dir for OpenCL headers")
endif()
######################################################################################
if(UNIX)
find_package(PkgConfig MODULE REQUIRED)
endif()
######################################################################################
set(ANDROID_COMPILER 0)
if(CMAKE_C_COMPILER MATCHES "android")
set(ANDROID_COMPILER 1)
add_definitions(-DPOCL_ANDROID)
add_definitions(-DPOCL_ANDROID_PREFIX="/data/data/org.pocl.libs/files")
endif()
######################################################################################
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
find_package( Hwloc )
if(NOT Hwloc_FOUND)
message(FATAL_ERROR "hwloc package is required!")
endif()
if("${Hwloc_VERSION}" VERSION_LESS "1.0")
message(FATAL_ERROR "Hwloc version must be >= 1.0 !")
endif()
message(STATUS "Hwloc_VERSION ${Hwloc_VERSION}")
message(STATUS "Hwloc_LDFLAGS ${Hwloc_LDFLAGS}")
message(STATUS "Hwloc_CFLAGS ${Hwloc_CFLAGS}")
######################################################################################
if(NOT HOST_CPU_CACHELINE_SIZE)
set(CL_SIZE 0)
if(UNIX)
find_program(GETCONF "getconf")
if(GETCONF)
execute_process(COMMAND "getconf" "LEVEL1_DCACHE_LINESIZE"
RESULT_VARIABLE RES OUTPUT_VARIABLE CL_SIZE)
if(RES)
message(WARNING "getconf exited with nonzero status!")
set(CL_SIZE 0)
else()
# getconf sometimes just returns zero
if(NOT (CL_SIZE EQUAL 0))
message(STATUS "L1D Cacheline size detected: ${CL_SIZE}")
set(HOST_CPU_CACHELINE_SIZE "${CL_SIZE}" CACHE STRING "L1D Cacheline size")
endif()
endif()
endif()
endif()
if(CL_SIZE EQUAL 0)
message(WARNING "Unable to detect cacheline size - assuming 64byte cacheline, override with -DHOST_CPU_CACHELINE_SIZE=<number> (Note: this is merely used for optimization, at worst pocl will be slightly slower)")
set(HOST_CPU_CACHELINE_SIZE "64" CACHE STRING "L1D Cacheline size")
endif()
endif()
######################################################################################
#
# Find executables to few tools required during build
#
find_program(PATCH_EXEC
NAMES patch${CMAKE_EXECUTABLE_SUFFIX}
HINTS ENV PATH
)
find_program(XARGS_EXEC
NAMES xargs${CMAKE_EXECUTABLE_SUFFIX}
HINTS ENV PATH
)
if(NOT PATCH_EXEC)
message(FATAL_ERROR "Could not find patch command.")
endif()
if(NOT XARGS_EXEC)
message(FATAL_ERROR "Could not find xargs command.")
endif()
######################################################################################
if (OCS_AVAILABLE)
include(LLVM RESULT_VARIABLE RES)
if(NOT RES)
message(FATAL_ERROR "Could not load LLVM.cmake")
endif()
if(NOT DEFINED HOST_DEVICE_BUILD_HASH)
if(KERNELLIB_HOST_CPU_VARIANTS STREQUAL "distro")
set(HOST_DEVICE_BUILD_HASH "${LLC_TRIPLE}")
else()
set(HOST_DEVICE_BUILD_HASH "${LLC_TRIPLE}-${LLC_HOST_CPU}")
endif()
endif()
if(ARM AND LLVM_3_9)
message(FATAL_ERROR "pocl does not build on ARM with LLVM 3.9 unless is was patched. Try LLVM 3.8 or (preferably) 4.0+")
endif()
if(INTEL_SDE_AVX512)
set(HOST_CPU_FORCED 1 CACHE INTERNAL "CPU is forced by user" FORCE)
set(LLC_HOST_CPU "skylake-avx512" CACHE STRING "The Host CPU to use with llc" FORCE)
endif()
else()
if(NOT DEFINED HOST_DEVICE_BUILD_HASH)
message(FATAL_ERROR "For compiler-less builds, you must define HOST_DEVICE_BUILD_HASH")
endif()
endif()
######################################################################################
if(ENABLE_HSA)
include(HSA RESULT_VARIABLE RES)
if(NOT RES)
message(FATAL_ERROR "Could not load HSA.cmake")
endif()
endif()
######################################################################################
if (NOT MSVC)
find_program(LINK_COMMAND
NAMES ld${CMAKE_EXECUTABLE_SUFFIX}
HINTS ENV PATH
)
else()
set(LINK_COMMAND "${CLANGXX}")
endif()
######################################################################################
# if variable FEATURE_X isn't defined, sets it to DEFAULT_FEATURE_X;
# also, if DEFAULT_FEATURE_X is 0, prevents FEATURE_X being 1
# since it takes DEFAULT_FEATURE_X=0 to mean "FEATURE_X is unavailable"
macro(setup_cached_var VARNAME DESCRIPTION DOCS_FEATURE_IS_UNAVAILABLE DOCS_REQUESTED_DISABLING_FEATURE)
if(DEFINED ${VARNAME})
set(_CACHED "(cached)")
else()
set(_CACHED "")
set(${VARNAME} ${DEFAULT_${VARNAME}})
endif()
if(${VARNAME} AND (NOT ${DEFAULT_${VARNAME}}))
message(WARNING "${DOCS_FEATURE_IS_UNAVAILABLE}")
set(${VARNAME} 0)
set(_CACHED "(override)")
endif()
if((NOT ${VARNAME}) AND ${DEFAULT_${VARNAME}} )
message(STATUS "${DOCS_REQUESTED_DISABLING_FEATURE}")
endif()
if(${VARNAME})
message(STATUS "${DESCRIPTION} ${_CACHED}: 1")
else()
message(STATUS "${DESCRIPTION} ${_CACHED}: 0")
endif()
endmacro()
######################################################################################
if(UNIX)
include(CheckCSourceCompiles)
include(CheckSymbolExists)
# don't allow implicit function declarations
set(CMAKE_REQUIRED_FLAGS "-std=c99 ${FORBIT_IMPLICIT_FUNCTIONS}")
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
set(CMAKE_REQUIRED_LIBRARIES "rt")
endif ()
CHECK_SYMBOL_EXISTS("fork"
"sys/types.h;unistd.h"
HAVE_FORK)
CHECK_SYMBOL_EXISTS("fsync"
"unistd.h"
HAVE_FSYNC)
CHECK_SYMBOL_EXISTS("utime"
"sys/types.h;utime.h"
HAVE_UTIME)
set(CMAKE_REQUIRED_DEFINITIONS "-D_POSIX_C_SOURCE=200809L")
CHECK_SYMBOL_EXISTS("futimens"
"fcntl.h;sys/stat.h"
HAVE_FUTIMENS)
set(CMAKE_REQUIRED_DEFINITIONS "-D_POSIX_C_SOURCE=200112L")
CHECK_SYMBOL_EXISTS("posix_memalign"
"stdlib.h"
HAVE_POSIX_MEMALIGN)
set(CMAKE_REQUIRED_DEFINITIONS "-D_POSIX_C_SOURCE=199309L")
CHECK_SYMBOL_EXISTS("clock_gettime"
"time.h"
HAVE_CLOCK_GETTIME)
CHECK_SYMBOL_EXISTS("fdatasync"
"unistd.h"
HAVE_FDATASYNC)
set(CMAKE_REQUIRED_DEFINITIONS "-D_BSD_SOURCE" "-D_DEFAULT_SOURCE")
CHECK_SYMBOL_EXISTS("mkdtemp"
"stdlib.h;unistd.h"
HAVE_MKDTEMP)
CHECK_SYMBOL_EXISTS("mkstemps"
"stdlib.h;unistd.h"
HAVE_MKSTEMPS)
CHECK_SYMBOL_EXISTS("vfork"
"sys/types.h;unistd.h"
HAVE_VFORK)
set(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
CHECK_SYMBOL_EXISTS("mkostemps"
"stdlib.h"
HAVE_MKOSTEMPS)
unset(CMAKE_REQUIRED_DEFINITIONS)
unset(CMAKE_REQUIRED_FLAGS)
else()
set(HAVE_CLOCK_GETTIME 0)
set(HAVE_FDATASYNC 0)
set(HAVE_FSYNC 0)
set(HAVE_MKOSTEMPS 0)
set(HAVE_MKSTEMPS 0)
set(HAVE_MKDTEMP 0)
set(HAVE_FUTIMENS 0)
set(HAVE_FORK 0)
set(HAVE_VFORK 0)
set(HAVE_UTIME 0)
endif()
######################################################################################
if((DEFINED ENABLE_VECMATHLIB) AND (DEFINED ENABLE_SLEEF))
if(ENABLE_SLEEF AND ENABLE_VECMATHLIB)
message(FATAL_ERROR "requested to use both Vecmathlib and SLEEF - pick one!")
endif()
if((NOT ENABLE_SLEEF) AND (NOT ENABLE_VECMATHLIB))
message(FATAL_ERROR "requested to use neither Vecmathlib nor SLEEF - pick one!")
endif()
endif()
if(NOT DEFINED ENABLE_CONFORMANCE)
if(((DEFINED ENABLE_VECMATHLIB) AND ENABLE_VECMATHLIB) OR
((DEFINED ENABLE_SLEEF) AND (NOT ENABLE_SLEEF)))
set(DEFAULT_CONF OFF)
set(DEFAULT_SLEEF OFF)
set(DEFAULT_VML ON)
else()
set(DEFAULT_CONF ON)
set(DEFAULT_SLEEF ON)
set(DEFAULT_VML OFF)
endif()
else()
set(DEFAULT_CONF ${ENABLE_CONFORMANCE})
if (ENABLE_CONFORMANCE)
# requested conformant
if((DEFINED ENABLE_VECMATHLIB) AND ENABLE_VECMATHLIB)
message(FATAL_ERROR "requested to use Vecmathlib with enabled conformance")
endif()
if((DEFINED ENABLE_SLEEF) AND (NOT ENABLE_SLEEF))
message(FATAL_ERROR "conformance needs enabled SLEEF")
endif()
set(DEFAULT_SLEEF ON)
set(DEFAULT_VML OFF)
else()
# requested non-conformant
set(DEFAULT_SLEEF ${ENABLE_SLEEF})
set(DEFAULT_VML ${ENABLE_VECMATHLIB})
# at least one
if((NOT DEFAULT_SLEEF) AND (NOT DEFAULT_VML))
set(DEFAULT_VML ON)
set(DEFAULT_SLEEF OFF)
endif()
endif()
endif()
option(ENABLE_CONFORMANCE "Enable conformance to OpenCL standard. Disabling this may enable slightly faster kernel library functions (at a price of range/precision). Note that enabling this does not guarantee conformance (depends on hardware). Incompatible with Vecmathlib. Defaults to ON" ${DEFAULT_CONF})
option(ENABLE_SLEEF "Use SLEEF for kernel library, mutually exclusive with ENABLE_VECMATHLIB" ${DEFAULT_SLEEF})
option(ENABLE_VECMATHLIB "Use vecmathlib for kernel library, mutually exclusive with ENABLE_SLEEF" ${DEFAULT_VML})
if((NOT CLANGXX_WORKS) AND ENABLE_VECMATHLIB)
message(WARNING "Disabling vecmathlib because clang++ doesn't seem to work!")
set(ENABLE_VECMATHLIB OFF)
set(ENABLE_VECMATHLIB OFF CACHE BOOL "VML" FORCE)
set(ENABLE_SLEEF ON)
set(ENABLE_SLEEF ON CACHE BOOL "SLEEF" FORCE)
endif()
# vecmathlib does not compile with fp16 currently
if(ENABLE_VECMATHLIB AND (NOT CL_DISABLE_HALF))
message(STATUS "Half available, but disabling half support since vecmathlib is enabled.")
set(CL_DISABLE_HALF 1)
set(CL_DISABLE_HALF 1 CACHE BOOL "Disable cl_khr_fp16 because fp16 is not supported")
endif()
######################################################################################
set(SPIRV OFF)
if((NOT LLVM_OLDER_THAN_5_0) AND CLANG_IS_PATCHED_FOR_SPIR_CC AND X86 AND (NOT KERNELLIB_HOST_CPU_VARIANTS STREQUAL "distro"))
option(ENABLE_SPIR "Enable SPIR support (default ON when available)" ON)
if(ENABLE_SPIR)
message(WARNING "SPIR support is available but highly experimental; use at your own risk.")
if(LLVM_SPIRV AND (EXISTS "${LLVM_SPIRV}"))
message(WARNING "SPIR-V support enabled but highly experimental; you must use a llvm-spirv "
"converter that produces bitcode FOR YOUR LLVM VERSION. "
"E.g. if you're compiling pocl against LLVM 5 then using Khronos' "
"llvm-spirv based on LLVM 3.6 branch WILL NOT WORK.")
set(SPIRV ON)
endif()
endif()
else()
set(ENABLE_SPIR OFF CACHE INTERNAL "SPIR enabled" FORCE)
endif()
set(ENABLE_SPIRV ${SPIRV} CACHE INTERNAL "SPIR-V enabled" FORCE)
######################################################################################
if(CLANG_HAS_RTLIB_128)
option(ENABLE_POCL_FLOAT_CONVERSION "Enable use of pocl's own float-to-decimal conversion code in OpenCL printf(). Defaults to OFF (uses snprintf from C library). Requires compiler-rt." OFF)
else()
set(ENABLE_POCL_FLOAT_CONVERSION OFF CACHE INTERNAL "rtlib")
endif()
unset(RTLIB_FLAG)
if(ENABLE_POCL_FLOAT_CONVERSION)
set(RTLIB_FLAG "-DENABLE_POCL_FLOAT_CONVERSION")
endif()
######################################################################################
# for kernel code, disable PIC & stack protector
#
# it seems PIC and stack-protector defaults somehow depend on
# clang build type or environment. PIC causes problems with
# constant addrspace variables, and stack protector likely slows
# down the kernels, so it needs to be determined whether it's worth
# the trouble.
set(DEFAULT_KERNEL_CL_FLAGS "-x cl -fno-stack-protector -fPIC ${RTLIB_FLAG}")
set(DEFAULT_KERNEL_C_FLAGS "-xc -std=c11 -D__CBUILD__ -fno-math-errno -fno-stack-protector -fPIC ${RTLIB_FLAG}")
set(DEFAULT_KERNEL_CXX_FLAGS "-xc++ -std=c++11 -fno-stack-protector -fPIC ${RTLIB_FLAG}")
if(ENABLE_VECMATHLIB)
option(USE_VECMATHLIB_BUILTINS_ONLY "Use only __builtin_* functions in the kernel library." OFF)
set(DEFAULT_KERNEL_CXX_FLAGS "${DEFAULT_KERNEL_CXX_FLAGS} -DVML_NO_IOSTREAM ${CLANGXX_STDLIB} ")
if(USE_VECMATHLIB_BUILTINS_ONLY)
set(DEFAULT_KERNEL_CL_FLAGS "${DEFAULT_KERNEL_CL_FLAGS} -DPOCL_VECMATHLIB_BUILTIN ")
set(DEFAULT_KERNEL_CXX_FLAGS "${DEFAULT_KERNEL_CXX_FLAGS} -DPOCL_VECMATHLIB_BUILTIN ")
endif()
endif()
set(EXTRA_KERNEL_FLAGS "" CACHE STRING "Extra arguments to all kernel compilation commands (defaults to empty)")
set(EXTRA_KERNEL_CL_FLAGS "" CACHE STRING "Extra arguments to kernel CL compiler (defaults to empty)")
set(EXTRA_KERNEL_CXX_FLAGS "" CACHE STRING "Extra arguments to kernel CXX compiler (defaults to empty)")
set(EXTRA_KERNEL_C_FLAGS "" CACHE STRING "Extra arguments to kernel C compiler (defaults to empty)")
set(KERNEL_CXX_FLAGS "${DEFAULT_KERNEL_CXX_FLAGS}${EXTRA_KERNEL_FLAGS}${EXTRA_KERNEL_CXX_FLAGS}")
set(KERNEL_CL_FLAGS "${DEFAULT_KERNEL_CL_FLAGS}${EXTRA_KERNEL_FLAGS}${EXTRA_KERNEL_CL_FLAGS}")
set(KERNEL_C_FLAGS "${DEFAULT_KERNEL_C_FLAGS}${EXTRA_KERNEL_FLAGS}${EXTRA_KERNEL_C_FLAGS}")
######################################################################################
if(UNIX)
if(APPLE)
# MacOS ld outputs useless warnings like
# ld: warning: -macosx_version_min not specificed, assuming 10.7
# suppress them with -w.
set(DEFAULT_HOST_LD_FLAGS "-dylib -w -lm")
elseif(ANDROID_COMPILER)
set(DEFAULT_HOST_LD_FLAGS "-L/system/lib/ -shared -ldl -lc -lm /system/lib/crtbegin_so.o /system/lib/crtend_so.o")
else()
if(ENABLE_POCL_FLOAT_CONVERSION)
set(DEFAULT_HOST_LD_FLAGS "-shared -lm --rtlib=compiler-rt")
else()
set(DEFAULT_HOST_LD_FLAGS "-shared -lm")
endif()
endif()
set(LIBMATH "-lm")
elseif(WIN32)
set(LIBMATH)
endif()
######################################################################################
if(UNIX)
if(APPLE)
# TODO MACOSX_BUNDLE target prop
set(ICD_LD_FLAGS "-single_module")
else()
set(ICD_LD_FLAGS "-Wl,-Bsymbolic")
endif()
endif()
######################################################################################
if (OCS_AVAILABLE)
option(SINGLE_LLVM_LIB "When on, tries to link pocl to the single big libLLVM before falling back to LLVM_LIBFILES)." ON)
if (DEFINED STATIC_LLVM)
message(AUTHOR_WARNING "STATIC_LLVM option was renamed, it wasn't really ensuring anything was static. Please see the SINGLE_LLVM_LIB option")
endif()
if(NOT SINGLE_LLVM_LIB)
message(STATUS "Linking LLVM to LLVM_LIBFILES")
set(POCL_LLVM_LIBS ${LLVM_LIBFILES})
else()
message(STATUS "Trying to link LLVM to the single big libLLVM")
find_library(LLVM_SHARED_LIB_FILE NAMES "LLVM-${LLVM_VERSION}" "LLVM" PATHS "${LLVM_LIBDIR}" NO_DEFAULT_PATH)
if(LLVM_SHARED_LIB_FILE AND EXISTS "${LLVM_SHARED_LIB_FILE}")
message(STATUS " .. OK, using ${LLVM_SHARED_LIB_FILE}")
set(POCL_LLVM_LIBS "${LLVM_SHARED_LIB_FILE}")
else()
message(STATUS "single big libLLVM library not found (Probably because LLVM is built with cmake). Falling back to linking libpocl to LLVM_LIBFILES")
set(POCL_LLVM_LIBS ${LLVM_LIBFILES})
set(SINGLE_LLVM_LIB OFF CACHE BOOL "single big libLLVM")
endif()
endif()
endif()
######################################################################################
include_directories("fix-include" "include")
######################################################################################
if(WIN32)
message(STATUS "Using LoadLibrary/FreeLibrary in Windows, libltdl not needed.")
else()
find_library(LTDL_LIB "ltdl")
find_file(LTDL_H "ltdl.h")
if(LTDL_LIB AND LTDL_H)
message(STATUS "ltdl found")
get_filename_component(LTDL_H_INCLUDE_DIR "${LTDL_H}" DIRECTORY)
string(FIND "${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}" "${LTDL_H_INCLUDE_DIR}" LTPOSITION)
# include the directory of ltdl.h, if its not in the default system include dirs
# also when cross-compiling this includes <cross-compile-root>/usr/include, which screws things up
if((LTPOSITION LESS "0") AND (NOT CMAKE_CROSSCOMPILING))
include_directories("${LTDL_H_INCLUDE_DIR}")
endif()
else()
message(FATAL_ERROR "Could not find LTDL library!")
endif()
endif()
######################################################################################
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
if(CMAKE_VERSION VERSION_GREATER "3.0.99")
set(PTHREAD_LIBRARY Threads::Threads)
else()
set(PTHREAD_LIBRARY ${CMAKE_THREAD_LIBS_INIT})
endif()
######################################################################################
# LTTNG
if(NOT MSVC)
pkg_check_modules(LTTNG_UST lttng-ust>=2.7)
if(LTTNG_UST_FOUND)
set(HAVE_LTTNG_UST 1)
else()
set(HAVE_LTTNG_UST 0)
endif()
endif()
######################################################################################
if(NOT DEFINED DEFAULT_ENABLE_ICD)
if (MSVC)
message(STATUS "Building ICD not yet supported on Windows.")
set(DEFAULT_ENABLE_ICD 0 CACHE INTERNAL "Going to use ICD loader")
else()
# pkg-config doesn't work with cross-compiling
if (NOT CMAKE_CROSSCOMPILING)
pkg_check_modules(OCL_ICD ocl-icd>=1.3)
endif()
if (CMAKE_CROSSCOMPILING OR (NOT OCL_ICD_FOUND))
find_path(OCL_ICD_INCLUDE_DIR
NAMES
ocl_icd.h
)
find_library(OCL_ICD_LIBRARIES
NAMES
OpenCL
)
if(OCL_ICD_INCLUDE_DIR AND OCL_ICD_LIBRARIES)
set(OCL_ICD_FOUND 1)
endif()
endif()
if(OCL_ICD_FOUND)
set(HAVE_OCL_ICD 1 CACHE INTERNAL "ICL library is ocl-icd")
set(OPENCL_FOUND 1 CACHE INTERNAL "opencl ICD/library found")
# duh, why doesn't ocl-icd set this in its .pc file ??
if (CMAKE_CROSSCOMPILING)
set(OPENCL_LIBRARIES "${OCL_ICD_LIBRARIES}" CACHE INTERNAL "opencl ICD/library link flags")
else()
separate_arguments(OCL_LDFLAGS UNIX_COMMAND "${OCL_ICD_LDFLAGS}")
list(APPEND OCL_LDFLAGS "OpenCL")
set(OPENCL_LIBRARIES "${OCL_LDFLAGS}" CACHE INTERNAL "opencl ICD/library link flags")
endif()
set(DEFAULT_ENABLE_ICD 1 CACHE INTERNAL "ICD loader availability")
else()
set(HAVE_OCL_ICD 0 CACHE INTERNAL "ICL library is ocl-icd")
# fallback to other ICD loaders
message(STATUS "ocl-icd not found -> trying fallback ICD implementations")
pkg_check_modules(OPENCL OpenCL>=1.2)
if(NOT OPENCL_FOUND)
find_library(OPENCL_LIBRARIES OpenCL)
# version check the found library
if(OPENCL_LIBRARIES)
set(CMAKE_REQUIRED_LIBRARIES "${OPENCL_LIBRARIES}")
include(CheckFunctionExists)
unset (OPENCL_FOUND CACHE)
CHECK_FUNCTION_EXISTS("clEnqueueFillImage" OPENCL_FOUND)
endif()
endif()
if(OPENCL_FOUND)
# no ocl-icd, but libopencl
message(STATUS "libOpenCL (unknown ICD loader) found")
set(DEFAULT_ENABLE_ICD 1 CACHE INTERNAL "ICD loader availability")
else()
message(STATUS "No ICD loader of any kind found (or its OpenCL version is <1.2)")
# no ocl-icd, no libopencl
set(DEFAULT_ENABLE_ICD 0 CACHE INTERNAL "no ICL loader found availability")
endif()
endif()
endif()
endif()
setup_cached_var(ENABLE_ICD "Using an ICD loader"
"Requested build with icd, but ICD loader not found! some examples will not work.."
"ICD loader found, but requested build without it")
if(ENABLE_ICD)
# only meaningful to link tests with ocl-icd
set(TESTS_USE_ICD ${HAVE_OCL_ICD})
set(POCL_LIBRARY_NAME "pocl")
else()
set(TESTS_USE_ICD 0)
set(POCL_LIBRARY_NAME "OpenCL")
endif()
message(STATUS "Run tests with ICD: ${TESTS_USE_ICD}")
######################################################################################
if(DEFINED INSTALL_OPENCL_HEADERS)
message(STATUS "Install POCL's OpenCL headers: ${INSTALL_OPENCL_HEADERS}")
else() # Undefined = auto -> check
find_file(OPENCL_H opencl.h PATH_SUFFIXES CL OpenCL)
if(OPENCL_H)
message(STATUS "OpenCL.h found, NOT installing our headers")
set(IOH 0)
else()
message(STATUS "OpenCL.h not found, installing our headers")
set(IOH 1)
endif()
set(INSTALL_OPENCL_HEADERS ${IOH} CACHE BOOL "Install POCL's OpenCL headers. (Ones from Kronos should be installed instead)")
endif()
######################################################################################
option(PEDANTIC "Compile host library with stricter compiler flags." OFF)
if(PEDANTIC)
add_compile_options("-Wno-unused-result" "-Werror") # maybe "-Wimplicit"
endif()
######################################################################################
set_expr(POCL_KERNEL_CACHE_DEFAULT KERNEL_CACHE_DEFAULT)
string(TIMESTAMP POCL_BUILD_TIMESTAMP "%d%m%Y%H%M%S")
file(WRITE "${CMAKE_BINARY_DIR}/pocl_build_timestamp.h" "#define POCL_BUILD_TIMESTAMP \"${POCL_BUILD_TIMESTAMP}\"")
####################################################################
# Host (basic/pthread) driver setup
set(DEFAULT_HOST_CLANG_FLAGS "${CLANG_TARGET_OPTION}${LLC_TRIPLE}")
set(DEFAULT_HOST_LLC_FLAGS "-relocation-model=pic -mtriple=${LLC_TRIPLE}")
if(ARM)
# 32bit arm doesn't always support doubles,
# need to enable FP64 manually with LLVM < 4.0
if(LLVM_OLDER_THAN_4_0)
option(ENABLE_FP64 "Enable FP64 on ARM32 - disabled by default for LLVM < 4" OFF)
else()
option(ENABLE_FP64 "Enable FP64 on ARM32 - if you have at least VFP support for doubles, you can leave it ON" ON )
endif()
else()
set(ENABLE_FP64 ON CACHE INTERNAL "FP64, always on except ARM")
endif()
if(ARM32 OR (LLC_TRIPLE MATCHES "^arm"))
if(LLC_TRIPLE MATCHES "gnueabihf")
# hardfloat
set(DEFAULT_HOST_LLC_FLAGS "${DEFAULT_HOST_LLC_FLAGS} -float-abi=hard")
set(DEFAULT_HOST_CLANG_FLAGS "${DEFAULT_HOST_CLANG_FLAGS} -mfloat-abi=hard")
set(DEFAULT_HOST_AS_FLAGS "${DEFAULT_HOST_AS_FLAGS} -mfloat-abi=hard")
else()
# softfloat
set(HOST_FLOAT_SOFT_ABI 1)
set(DEFAULT_HOST_LLC_FLAGS "${DEFAULT_HOST_LLC_FLAGS} -float-abi=soft")
set(DEFAULT_HOST_CLANG_FLAGS "${DEFAULT_HOST_CLANG_FLAGS} -mfloat-abi=soft")
set(DEFAULT_HOST_AS_FLAGS "${DEFAULT_HOST_AS_FLAGS} -mfloat-abi=soft")
endif()
endif()
if(CL_DISABLE_HALF)
set(DEFAULT_HOST_CLANG_FLAGS "${DEFAULT_HOST_CLANG_FLAGS} -D_CL_DISABLE_HALF")
endif()
set(HOST_DEVICE_CL_VERSION "120")
set(HOST_DEVICE_CL_STD "1.2")
# define it here, b/c we'll need these both at runtime and buildtime
if(X86 OR ARM)
set(HOST_DEVICE_EXTENSIONS "cl_khr_byte_addressable_store cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_3d_image_writes")
else()
# set some conservative defaults
set(HOST_DEVICE_EXTENSIONS "cl_khr_global_int32_base_atomics cl_khr_local_int32_base_atomics cl_khr_3d_image_writes")
endif()
if(ENABLE_SPIR)
set(HOST_DEVICE_EXTENSIONS "${HOST_DEVICE_EXTENSIONS} cl_khr_spir")
endif()
if(NOT CL_DISABLE_HALF)
set(HOST_DEVICE_EXTENSIONS "${HOST_DEVICE_EXTENSIONS} cl_khr_fp16")
endif()
# must not be defined in HOST_DEVICE_EXTENSIONS list, because
# this extension doesn't exist in official extension list
# there is "cles_khr_int64" which indicates int64 support for embedded profiles
set(HOST_DEVICE_EXTENSION_DEFINES "-Dcl_khr_int64")
if(X86)
set(HOST_DEVICE_EXTENSIONS "${HOST_DEVICE_EXTENSIONS} cl_khr_fp64 cl_khr_int64_base_atomics cl_khr_int64_extended_atomics")
endif()
if(ENABLE_FP64)
set(HOST_DEVICE_EXTENSIONS "${HOST_DEVICE_EXTENSIONS} cl_khr_fp64")
set(_CL_DISABLE_DOUBLE 0)
else()
set(_CL_DISABLE_DOUBLE 1)
endif()
set(TEMP_EXT "${HOST_DEVICE_EXTENSIONS}")
separate_arguments(TEMP_EXT)
set(TEMP_CLEXT "-Xclang -cl-ext=-all,")
foreach(EXT ${TEMP_EXT})
set(HOST_DEVICE_EXTENSION_DEFINES "${HOST_DEVICE_EXTENSION_DEFINES} -D${EXT}")
set(TEMP_CLEXT "${TEMP_CLEXT}+${EXT},")