summaryrefslogtreecommitdiffstats
path: root/contrib/cmake/FindSWI.cmake
blob: acea32c1bc8e9c10ea4b48e46103193605b6cf98 (plain)
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
set(64BIT_HOST OFF)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
	set(64BIT_HOST ON)
endif()

set(SWI_PLATFORM_ID)
if (UNIX)
	if (APPLE AND 64BIT_HOST)
		set(SWI_PLATFORM_ID "x86_64-${CMAKE_SYSTEM_NAME}${CMAKE_SYSTEM_VERSION}")
	else()
		set(SWI_PLATFORM_ID "${CMAKE_SYSTEM_PROCESSOR}-${CMAKE_SYSTEM_NAME}${CMAKE_SYSTEM_VERSION}")
	endif()
else()
	set(SWI_PLATFORM_ID "${CMAKE_SYSTEM_PROCESSOR}-windows")
endif()

if (SWI_PLATFORM_ID)
	string(TOLOWER ${SWI_PLATFORM_ID} SWI_PLATFORM_ID)
endif()

#message("SWI_PLATFORM_ID: ${SWI_PLATFORM_ID}")

set (SWI_SEARCH_PATHS)
list (APPEND SWI_SEARCH_PATHS 
	$ENV{SWI_HOME}
	${CMAKE_FIND_ROOT_PATH}
	"/usr/lib/swi-prolog/"
	"/opt/local/"
	"/usr/local/"
	"C:/Program Files (x86)/swipl"
	"C:/Program Files/swipl"
)

#message("SWI_SEARCH_PATHS: ${SWI_SEARCH_PATHS}")

set (SWI_VERSION)
set (LOOP_DONE 0)
foreach(SWI_SEARCH_PATH ${SWI_SEARCH_PATHS})
	if(NOT LOOP_DONE)
		file(GLOB SWI_VERSIONS ${SWI_SEARCH_PATH}/lib/swipl*)
		if (SWI_VERSIONS)
#			message("SWI_VERSIONS: ${SWI_VERSIONS}")
			set(LOOP_DONE 1)
			list(SORT SWI_VERSIONS)
			list(REVERSE SWI_VERSIONS)
			list(GET SWI_VERSIONS 0 SWI_VERSION)
			STRING(REGEX REPLACE ".*(([0-9]+).([0-9]+).([0-9]+))$" "\\1" SWI_VERSION "${SWI_VERSION}")
		endif()
	endif()
endforeach()

#message("SWI_VERSION: ${SWI_VERSION}")

set(SWI_PLATFORMS)
foreach (SWI_SEARCH_PATH ${SWI_SEARCH_PATHS})
	file(GLOB CURR_SWI_PLATFORMS ${SWI_SEARCH_PATH}/lib/swipl-${SWI_VERSION}/lib/*)
	foreach(CURR_SWI_PLATFORM ${CURR_SWI_PLATFORMS})
  	if(IS_DIRECTORY ${CURR_SWI_PLATFORM})
			list(APPEND SWI_PLATFORMS ${CURR_SWI_PLATFORM})
		endif()
	endforeach()
endforeach()
#message(FATAL_ERROR "SWI_PLATFORMS: ${SWI_PLATFORMS}")

#message("SWI_VERSION: ${SWI_VERSION}")

# -- find prolog headers
FIND_PATH(SWI_INCLUDE_DIR SWI-Prolog.h
  PATH_SUFFIXES 
		include
		lib/swipl-${SWI_VERSION}/include
  PATHS ${SWI_SEARCH_PATHS}
)

#message("SWI_INCLUDE_DIR: ${SWI_INCLUDE_DIR}")

FIND_PATH(SWI_CPP_INCLUDE_DIR SWI-cpp.h
  PATH_SUFFIXES 
		packages/cpp 
		lib/swipl-${SWI_VERSION}/include
  PATHS ${SWI_SEARCH_PATHS}
)

#message("SWI_CPP_INCLUDE_DIR: ${SWI_CPP_INCLUDE_DIR}")

FIND_PROGRAM(SWI_BINARY swipl
  PATH_SUFFIXES 
		src
		lib/swipl-${SWI_VERSION}/bin/${SWI_PLATFORM_ID}
  PATHS ${SWI_SEARCH_PATHS}
)

FIND_LIBRARY(SWI_LIBRARY_RELEASE
  NAMES libswipl swipl
  PATH_SUFFIXES
		lib/${SWI_PLATFORM_ID}                            # still in source directory
		lib/swipl-${SWI_VERSION}/lib/${SWI_PLATFORM_ID}   # after make install
		
	PATHS ${SWI_SEARCH_PATHS} ${SWI_PLATFORMS}
)

FIND_LIBRARY(SWI_LIBRARY_DEBUG
  NAMES libswipl_d swipl_d
  PATH_SUFFIXES
		lib/${SWI_PLATFORM_ID}                     # still in source directory
		lib/swipl-${SWI_VERSION}/lib/${SWI_PLATFORM_ID}     # after make install
	PATHS ${SWI_SEARCH_PATHS} ${SWI_PLATFORMS}
)

if (NOT SWI_LIBRARY_DEBUG) # no explicit debug build, just reuse release
	if (UNIX)
		set(SWI_LIBRARY_DEBUG ${SWI_LIBRARY_RELEASE})
	endif()
endif()

if (SWI_LIBRARY_RELEASE)
	list(APPEND SWI_LIBRARY optimized ${SWI_LIBRARY_RELEASE})
	if (SWI_LIBRARY_DEBUG)
		list(APPEND SWI_LIBRARY debug ${SWI_LIBRARY_DEBUG})
	elseif(UNIX)
		list(APPEND SWI_LIBRARY debug ${SWI_LIBRARY_RELEASE})
	else()
		message(FATAL_ERROR "Cannot find debug version of SWI")
	endif()
endif()

#message(FATAL_ERROR "SWI_BINARY: ${SWI_BINARY} / SWI_LIBRARY_RELEASE: ${SWI_LIBRARY_RELEASE} / SWI_LIBRARY_DEBUG: ${SWI_LIBRARY_DEBUG} / SWI_INCLUDE_DIR: ${SWI_INCLUDE_DIR} / SWI_CPP_INCLUDE_DIR: ${SWI_CPP_INCLUDE_DIR}")

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SWI DEFAULT_MSG SWI_LIBRARY SWI_BINARY SWI_INCLUDE_DIR SWI_CPP_INCLUDE_DIR)
MARK_AS_ADVANCED(SWI_LIBRARY SWI_INCLUDE_DIR)