forked from pocl/pocl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
1892 lines (1531 loc) · 68.9 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-2018 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 3.9 FATAL_ERROR)
cmake_policy(SET CMP0067 OLD)
project(pocl)
set(CMAKE_PROJECT_DESCRIPTION "pocl is a portable OpenCl runtime.")
set(LATEST_KNOWN_CXX_STD_VERSION "20")
set(SUPPORTED_CXX_STD_VERSION "11")
option(ENABLE_LATEST_CXX_STD "Upgrade C++ standard version to ${LATEST_KNOWN_CXX_STD_VERSION}. Required to get rid of unused variables warnings in compilers not supporting [[gnu::*]] attributes. Can bring other benefits, including performance and efficiency ones. Before a pull request build with this disabled." OFF)
if(ENABLE_LATEST_CXX_STD)
set(CMAKE_CXX_STANDARD "${LATEST_KNOWN_CXX_STD_VERSION}")
else()
set(CMAKE_CXX_STANDARD "${SUPPORTED_CXX_STD_VERSION}")
endif()
include(CheckCCompilerFlag)
include(CPackComponent)
macro(pass_through_cpack_vars)
get_cmake_property(cpackVarsToPassthrough VARIABLES)
foreach(varName ${cpackVarsToPassthrough})
if(varName MATCHES "^CPACK_DEBIAN_")
message(STATUS "${varName}")
set("${varName}" "${${varName}}" PARENT_SCOPE)
endif()
endforeach()
endmacro()
# don't allow implicit function declarations
if(UNIX)
if((CMAKE_C_COMPILER_ID STREQUAL "GNU") OR
(CMAKE_C_COMPILER_ID STREQUAL "Clang"))
add_compile_options("$<$<COMPILE_LANGUAGE:C>:-Werror=implicit-function-declaration>")
check_c_compiler_flag("-Wincompatible-pointer-types" HAVE_WARN_INCOMPATIBLE_POINTER_TYPES)
if (HAVE_WARN_INCOMPATIBLE_POINTER_TYPES)
add_compile_options("$<$<COMPILE_LANGUAGE:C>:-Wincompatible-pointer-types>")
endif()
add_compile_options("-Wno-ignored-attributes")
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 9)
set(VERSION_SUFFIX_FIXED_TEXT "-pre")
set(VERSION_SUFFIX "${VERSION_SUFFIX_FIXED_TEXT}")
set(VERSION_STRING ${MAJOR_VERSION}.${MINOR_VERSION}${VERSION_SUFFIX})
set(POCL_VERSION_BASE ${VERSION_STRING})
# required b/c SHARED libs defaults to ON while OBJECT defaults to OFF
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# CMake doesn't add "-pie" by default for executables (CMake issue #14983)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie")
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)
execute_process(COMMAND "${GIT_CMD}" "rev-parse" "HEAD"
OUTPUT_VARIABLE GIT_COMMIT
RESULT_VARIABLE EXITCODE
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
if(HAVE_GIT AND (VERSION_SUFFIX MATCHES "pre") AND (EXITCODE EQUAL 0))
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 "${VERSION_SUFFIX_FIXED_TEXT} ${GIT_DESCRIBE}")
set(VERSION_STRING ${MAJOR_VERSION}.${MINOR_VERSION}${VERSION_SUFFIX})
set(POCL_VERSION_FULL "${VERSION_STRING}")
else()
message(STATUS "No git and/or not a prerelease -> not adding git commit to version.")
set(POCL_VERSION_FULL "${POCL_VERSION_BASE}")
endif()
set(CPACK_PACKAGE_NAME pocl)
set(CPACK_PACKAGE_VENDOR pocl)
set(CPACK_PACKAGE_VERSION_MAJOR "${MAJOR_VERSION}")
set(CPACK_PACKAGE_VERSION_MINOR "${MINOR_VERSION}")
set(CPACK_PACKAGE_VERSION "${MAJOR_VERSION}.${MINOR_VERSION}~${VERSION_SUFFIX_FIXED_TEXT}")
if(HAVE_GIT)
set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}.${GIT_COMMIT}")
endif()
##################################################################################
if(DEFINED OCS_AVAILABLE)
message(WARNING "The OCS_AVAILABLE option is deprecated, since it actually meant 'LLVM available', but LLVM is not the only way to get compiler support in a device. Please use ENABLE_LLVM in future if you want to enable/disable building pocl against LLVM.")
set(ENABLE_LLVM ${OCS_AVAILABLE} CACHE BOOL "build against LLVM" FORCE)
else()
option(ENABLE_LLVM "Build pocl with LLVM. Default is ON." ON)
endif()
option(STATIC_LLVM "If ON, link to static LLVM libraries. OFF (default) = link to shared LLVM libraries." OFF)
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_LOADABLE_DRIVERS "Enable drivers to be dlopen()-ed at pocl runtime, instead of being linked into libpocl" ON)
option(ENABLE_HSA "Enable the HSA base profile runtime device driver" OFF)
option(ENABLE_CUDA "Enable the CUDA device driver for NVIDIA devices" OFF)
option(ENABLE_VULKAN "Experimental and incomplete driver that uses the Vulkan API for controlling the device. Please refer to the user manual for the status and open tasks" OFF)
option(ENABLE_PROXY_DEVICE "Enable proxy driver for proxying to another OpenCL implementation" OFF)
option(ENABLE_PROXY_DEVICE_INTEROP "Enable OpenGL- or EGL-interop with the proxy driver" OFF)
option(KERNEL_CACHE_DEFAULT "Default value for the kernel compile cache. If disabled, pocl will still use kernel cache for intermediate compilation files, but will clean up them 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)
if (ENABLE_PROXY_DEVICE)
set(VISIBILITY_HIDDEN_DEFAULT OFF)
else()
set(VISIBILITY_HIDDEN_DEFAULT ON)
endif()
option(VISIBILITY_HIDDEN "Build with -fvisibility=hidden -fvisibility-inlines-hidden" ${VISIBILITY_HIDDEN_DEFAULT})
if(VISIBILITY_HIDDEN)
add_compile_options(-fvisibility=hidden)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fvisibility-inlines-hidden>)
endif()
# Ninja Job Pool support
set(PARALLEL_COMPILE_JOBS "" CACHE STRING
"Define the maximum number of concurrent compilation jobs (Ninja only).")
if(PARALLEL_COMPILE_JOBS)
if(CMAKE_GENERATOR STREQUAL "Ninja")
set_property(GLOBAL APPEND PROPERTY JOB_POOLS compile_job_pool=${PARALLEL_COMPILE_JOBS})
set(CMAKE_JOB_POOL_COMPILE compile_job_pool)
endif()
endif()
set(PARALLEL_LINK_JOBS "" CACHE STRING
"Define the maximum number of concurrent link jobs (Ninja only).")
if(CMAKE_GENERATOR STREQUAL "Ninja")
if(PARALLEL_LINK_JOBS)
set_property(GLOBAL APPEND PROPERTY JOB_POOLS link_job_pool=${PARALLEL_LINK_JOBS})
set(CMAKE_JOB_POOL_LINK link_job_pool)
endif()
endif()
if(NOT CMAKE_GENERATOR STREQUAL "Ninja" AND (PARALLEL_COMPILE_JOBS OR PARALLEL_LINK_JOBS))
message(WARNING "Job pooling is only available with Ninja generators.")
endif()
#### these are mostly useful for pocl developers
option(ENABLE_EXTRA_VALIDITY_CHECKS "Enable extra checks on cl_* object validity" OFF)
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)
option(ENABLE_HOST_CPU_DEVICES "Add host CPUs as OpenCL devices (basic and pthread)." ON)
option(ENABLE_ACCEL_DEVICE "Enable the generic hardware accelerator device driver." OFF)
option(ENABLE_POCLCC "Build poclcc. Defaults to ON" ON)
option(ENABLE_TESTS "Build tests. Defaults to ON" ON)
option(ENABLE_EXAMPLES "Build examples. Defaults to ON" ON)
##########################################################
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(HOST_DEVICE_ADDRESS_BITS 64)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(HOST_DEVICE_ADDRESS_BITS 32)
else()
message(FATAL_ERROR "Cannot figure out HOST_DEVICE_ADDRESS_BITS")
endif()
# printf buffer size, in KB
if(NOT DEFINED PRINTF_BUFFER_SIZE)
set(PRINTF_BUFFER_SIZE 16384 CACHE STRING "printf buffer size, in KB")
endif()
##################################################################################
if(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le")
set(POWERPC 1)
set(POWERPC64LE 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc")
set(POWERPC 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "mips")
set(MIPS 1)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(arm|aarch64)")
set(ARM 1)
if(HOST_DEVICE_ADDRESS_BITS MATCHES "32")
set(ARM32 1)
else()
set(ARM64 1)
endif()
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(i.86|AMD64|x86_64|amd64)")
set(X86 1)
if(HOST_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()
include(GNUInstallDirs)
# for libpocl.so
set(POCL_INSTALL_PUBLIC_LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}" CACHE PATH "POCL public libdir")
# for libpocl-devices-*.so
set(POCL_INSTALL_PRIVATE_LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}/pocl" CACHE PATH "POCL private libdir")
# for pocl.icd
set(POCL_INSTALL_ICD_VENDORDIR "${CMAKE_INSTALL_FULL_SYSCONFDIR}/OpenCL/vendors" CACHE PATH "POCL ICD file destination")
# for kernel-<target>.bc
set(POCL_INSTALL_PRIVATE_DATADIR "${CMAKE_INSTALL_FULL_DATADIR}/pocl" CACHE PATH "POCL private datadir")
# for poclu.h
set(POCL_INSTALL_PUBLIC_HEADER_DIR "${CMAKE_INSTALL_FULL_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_FULL_BINDIR}" CACHE PATH "POCL public bindir")
# for PoclConfig.cmake & stuff
set(POCL_INSTALL_CMAKE_CONFIG_DIR "${POCL_INSTALL_PRIVATE_LIBDIR}/cmake" 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()
######################################################################################
######################################################################################
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
option(HARDENING_ENABLE "Enable hardening against various attacks. May worsen performance" OFF)
if(HARDENING_ENABLE)
include(Hardening)
else()
function(harden target)
endfunction()
endif()
find_package(PkgConfig MODULE)
find_package(Hwloc)
if(NOT Hwloc_FOUND)
message(STATUS "hwloc package not found")
set(ENABLE_HWLOC OFF CACHE BOOL "Hwloc" FORCE)
else()
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_LIBRARIES ${Hwloc_LIBRARIES}")
message(STATUS "Hwloc_INCLUDE_DIRS ${Hwloc_INCLUDE_DIRS}")
set(ENABLE_HWLOC ON CACHE BOOL "Hwloc" FORCE)
endif()
include(sanitizers)
######################################################################################
if(NOT HOST_CPU_CACHELINE_SIZE)
set(CL_SIZE 0)
if(UNIX OR CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|Darwin")
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 may in rare conditions return "undefined" value
if (CL_SIZE STREQUAL "undefined\n")
set(CL_SIZE 0)
endif()
# getconf sometimes just returns zero
if(NOT (CL_SIZE EQUAL 0))
string(STRIP "${CL_SIZE}" CL_SIZE)
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
HINTS ENV PATH
)
find_program(XARGS_EXEC
NAMES xargs
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(ENABLE_LLVM)
# this must be done before including LLVM,
# so we can check that TCE's LLVM and the found LLVM is the same.
if(NOT WITH_TCE)
set(WITH_TCE ENV PATH)
endif()
find_program(TCE_CONFIG NAMES "tce-config" HINTS ${WITH_TCE})
find_program(TCECC NAMES "tcecc" HINTS ${WITH_TCE})
find_program(TTASIM NAMES "ttasim" HINTS ${WITH_TCE})
include(LLVM RESULT_VARIABLE RES)
if(NOT RES)
message(FATAL_ERROR "Could not load LLVM.cmake")
endif()
if(ENABLE_HOST_CPU_DEVICES)
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(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()
endif()
else()
if(ENABLE_HOST_CPU_DEVICES AND (NOT DEFINED HOST_DEVICE_BUILD_HASH))
message(FATAL_ERROR "For compiler-less builds of CPU backend, 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")
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("sleep"
"unistd.h"
HAVE_SLEEP)
CHECK_SYMBOL_EXISTS("getrlimit"
"sys/time.h;sys/resource.h"
HAVE_GETRLIMIT)
CHECK_SYMBOL_EXISTS("utime"
"sys/types.h;utime.h"
HAVE_UTIME)
CHECK_SYMBOL_EXISTS("ANNOTATE_HAPPENS_BEFORE"
"valgrind/helgrind.h"
HAVE_VALGRIND)
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)
set(CMAKE_REQUIRED_LIBRARIES "dl")
CHECK_SYMBOL_EXISTS("dladdr"
"dlfcn.h"
HAVE_DLADDR)
unset(CMAKE_REQUIRED_DEFINITIONS)
unset(CMAKE_REQUIRED_FLAGS)
unset(CMAKE_REQUIRED_LIBRARIES)
else()
set(HAVE_CLOCK_GETTIME 0)
set(HAVE_FDATASYNC 0)
set(HAVE_FSYNC 0)
set(HAVE_SLEEP 0)
set(HAVE_MKOSTEMPS 0)
set(HAVE_MKSTEMPS 0)
set(HAVE_MKDTEMP 0)
set(HAVE_FUTIMENS 0)
set(HAVE_FORK 0)
set(HAVE_GETRLIMIT 0)
set(HAVE_VFORK 0)
set(HAVE_UTIME 0)
set(HAVE_DLADDR 0)
set(HAVE_VALGRIND 0)
endif()
######################################################################################
if(UNIX AND ENABLE_LLVM AND HAVE_DLADDR)
option(ENABLE_RELOCATION "make libpocl relocatable" ON)
else()
message(STATUS "Relocation not available")
set(ENABLE_RELOCATION OFF CACHE INTERNAL "libpocl relocatable" FORCE)
endif()
if(ENABLE_RELOCATION)
file(RELATIVE_PATH POCL_INSTALL_PRIVATE_DATADIR_REL ${POCL_INSTALL_PUBLIC_LIBDIR} ${POCL_INSTALL_PRIVATE_DATADIR})
message(STATUS "Private Datadir Relative path: ${POCL_INSTALL_PRIVATE_DATADIR_REL}")
install(FILES ${CLANG_OPENCL_HEADERS}
DESTINATION "${POCL_INSTALL_PRIVATE_DATADIR}/include" COMPONENT "dev")
endif()
file(RELATIVE_PATH POCL_INSTALL_PRIVATE_LIBDIR_REL ${POCL_INSTALL_PUBLIC_LIBDIR} ${POCL_INSTALL_PRIVATE_LIBDIR})
######################################################################################
# IPO support for runtime library
if(POLICY CMP0069)
cmake_policy(SET CMP0069 NEW)
endif()
if(NOT DEFINED DEFAULT_ENABLE_IPO)
set(DEFAULT_ENABLE_IPO OFF CACHE BOOL "IPO" FORCE)
if(NOT CMAKE_VERSION VERSION_LESS "3.9")
include(CheckIPOSupported)
check_ipo_supported(RESULT IPO OUTPUT IPO_OUTPUT)
set(DEFAULT_ENABLE_IPO ${IPO} CACHE BOOL "IPO" FORCE)
message(STATUS "Compiler supports IPO: ${DEFAULT_ENABLE_IPO}")
#message(STATUS "IPO check message: ${IPO_OUTPUT}")
endif()
endif()
setup_cached_var(ENABLE_IPO "Enable Link-Time Optimization (IPO) while building pocl runtime"
"Requested build with IPO, but IPO is not available"
"IPO available, but requested build without it")
######################################################################################
option(ENABLE_SLEEF "Use SLEEF for kernel library" ON)
option(ENABLE_CONFORMANCE "Enable conformance to OpenCL standard. \
Enabling this option this does not guarantee conformance (depends on hardware), \
but CMake will give errors if options that conflict with conformance are used" OFF)
if(ENABLE_CONFORMANCE AND (NOT ENABLE_SLEEF))
message(FATAL_ERROR "conformance needs enabled SLEEF")
endif()
######################################################################################
# fully device-side printf on devices which support it (only CPU backend ATM), disabled by default.
# this requires 128bit integer support because of the code in "errol" float-to-string conversion routine
# the output is not 100% compatible with glibc's printf (%f with large argument prints zeroes after
# last significant digit - 16-18th digit or so, unlike glibc which prints digits up to decimal point).
if(CLANG_HAS_128B_MATH)
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 "pocl's own float-to-decimal conversion code")
endif()
unset(FLOATCONV_FLAG)
if(ENABLE_POCL_FLOAT_CONVERSION)
# force link with Clang; otherwise not needed on x86 but in this case we need rtlib
set(FLOATCONV_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 "-xcl -fno-stack-protector -fPIC ${FLOATCONV_FLAG}")
set(DEFAULT_KERNEL_C_FLAGS "-xc -std=c11 -D__CBUILD__ -fno-math-errno -fno-stack-protector -fPIC ${FLOATCONV_FLAG}")
set(DEFAULT_KERNEL_CXX_FLAGS "-xc++ -std=c++11 -fno-stack-protector -fPIC ${FLOATCONV_FLAG}")
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 "-dynamiclib -w -lm")
elseif(ANDROID)
set(DEFAULT_HOST_LD_FLAGS "-L/system/lib/ -shared -ldl -lc /system/lib/crtbegin_so.o /system/lib/crtend_so.o")
else()
set(DEFAULT_HOST_LD_FLAGS "-shared")
endif()
set(LIBMATH "-lm")
elseif(WIN32)
set(LIBMATH)
endif()
if(CLANG_NEEDS_RTLIB)
set(DEFAULT_HOST_LD_FLAGS "${DEFAULT_HOST_LD_FLAGS} --rtlib=compiler-rt")
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()
######################################################################################
set(SPIRV OFF)
if(ENABLE_LLVM AND X86 AND (NOT KERNELLIB_HOST_CPU_VARIANTS STREQUAL "distro")
AND (NOT ENABLE_CONFORMANCE))
option(ENABLE_SPIR "Enable SPIR support (default ON when available)" ON)
else()
set(ENABLE_SPIR OFF CACHE INTERNAL "SPIR enabled" FORCE)
endif()
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()
set(ENABLE_SPIRV ${SPIRV} CACHE INTERNAL "SPIR-V enabled" FORCE)
if(ENABLE_CONFORMANCE AND (ENABLE_SPIR OR ENABLE_SPIRV))
message(FATAL_ERROR "conformance needs SPIR AND SPIR-V support disabled")
endif()
######################################################################################
set(HAVE_DLFCN_H OFF CACHE BOOL "dlopen" FORCE)
if(WIN32 AND (NOT MINGW))
message(STATUS "Using LoadLibrary/FreeLibrary in Windows, libltdl not needed.")
elseif(UNIX)
if (CMAKE_CROSSCOMPILING AND (NOT ENABLE_HOST_CPU_DEVICES) AND (NOT ENABLE_HSA))
message(STATUS "Cross-compiling without CPU/HSA devices -> skipping LIBDL search")
else()
find_library(DL_LIB "dl")
find_file(DL_H "dlfcn.h")
if(DL_LIB AND DL_H)
message(STATUS "libdl found")
else()
message(STATUS "libdl not found, assuming dlopen() is in libc")
set(DL_LIB "")
endif()
if(DL_H)
get_filename_component(DL_H_INCLUDE_DIR "${DL_H}" DIRECTORY)
string(FIND "${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}" "${DL_H_INCLUDE_DIR}" LTPOSITION)
# include the directory of dlfcn.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("${DL_H_INCLUDE_DIR}")
endif()
set(HAVE_DLFCN_H ON CACHE BOOL "dlfcn.h" FORCE)
else()
message(FATAL_ERROR "Could not find dlfcn.h!")
endif()
endif()
else()
message(STATUS "Unknown OS, don't know how to load a dynamic library")
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(UNIX)
if(PKG_CONFIG_EXECUTABLE)
pkg_check_modules(LTTNG_UST lttng-ust>=2.7)
endif()
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(PKG_CONFIG_EXECUTABLE)
pkg_check_modules(OCL_ICD ocl-icd>=1.3)
endif()
if (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")
set(OPENCL_LIBRARIES "OpenCL" CACHE INTERNAL "ocl-icd library")
set(OPENCL_LIBDIR "${OCL_ICD_LIBDIR}" CACHE INTERNAL "opencl ICD/library path")
set(DEFAULT_ENABLE_ICD 1 CACHE INTERNAL "ICD loader availability")
if(DEFINED OCL_ICD_VERSION AND (OCL_ICD_VERSION VERSION_GREATER_EQUAL "2.3.0"))
set(HAVE_OCL_ICD_30_COMPATIBLE 1 CACHE INTERNAL "ICD 3.0 compat")
else()
set(HAVE_OCL_ICD_30_COMPATIBLE 0 CACHE INTERNAL "ICD 3.0 compat")
endif()
else()
set(HAVE_OCL_ICD 0 CACHE INTERNAL "OCL library is ocl-icd")
unset (OPENCL_FOUND CACHE)
# fallback to other ICD loaders
message(STATUS "ocl-icd not found -> trying fallback ICD implementations")
if(PKG_CONFIG_EXECUTABLE)
pkg_check_modules(OPENCL OpenCL>=1.2)
endif()
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})
else()
set(TESTS_USE_ICD 0)
endif()
if(ENABLE_ICD OR ENABLE_PROXY_DEVICE)
set(POCL_LIBRARY_NAME "pocl")
else()
set(POCL_LIBRARY_NAME "OpenCL")
endif()
message(STATUS "Run tests with ICD: ${TESTS_USE_ICD}")
######################################################################################
if(APPLE)
find_file(OPENCL_H opencl.h PATH_SUFFIXES OpenCL CL)
find_file(OPENCL_HPP opencl.hpp PATH_SUFFIXES OpenCL CL)
else()
find_file(OPENCL_H opencl.h PATH_SUFFIXES CL)
find_file(OPENCL_HPP opencl.hpp PATH_SUFFIXES CL)
endif()
# if ICD is disabled, we might as well compile with our own headers
if(OPENCL_H AND ENABLE_ICD)
message(STATUS "OpenCL.h found (${OPENCL_H}) and ICD enabled, NOT installing our headers")
set(HAVE_OPENCL_H ON)
set(IOH OFF)
else()
message(STATUS "OpenCL.h not found or ICD disabled, installing our headers")
set(HAVE_OPENCL_H OFF)
set(IOH ON)
endif()
if(NOT DEFINED INSTALL_OPENCL_HEADERS)
option(INSTALL_OPENCL_HEADERS "Install POCL's OpenCL headers. (Ones from Khronos should be installed instead)" ${IOH})
endif()
add_definitions(-DCL_USE_DEPRECATED_OPENCL_1_0_APIS -DCL_USE_DEPRECATED_OPENCL_1_1_APIS
-DCL_USE_DEPRECATED_OPENCL_1_2_APIS -DCL_USE_DEPRECATED_OPENCL_2_0_APIS
-DCL_USE_DEPRECATED_OPENCL_2_1_APIS -DCL_USE_DEPRECATED_OPENCL_2_2_APIS)