1#
2# Collection of common macros and code for introspecting the platform.
3
4include(ProcessorCount)
5
6# Returns a simple string describing the current architecture. Possible
7# return values currently include: amd64, x86_64, x86.
8MACRO (_DETERMINE_ARCH var)
9  IF (DEFINED CB_DOWNLOAD_DEPS_ARCH)
10    SET (_arch ${CB_DOWNLOAD_DEPS_ARCH})
11  ELSEIF (DEFINED ENV{target_arch})
12    # target_arch is used by environment.bat to represent the desired
13    # target architecture, so use that value first if set.
14    STRING (TOLOWER "$ENV{target_arch}" _arch)
15  ELSE (DEFINED CB_DOWNLOAD_DEPS_ARCH)
16    # We tweak MacOS, which for some reason claims to be i386
17    IF (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
18      # QQQ MacOS 10.7 could be 32-bit; we should catch and abort
19      SET (_arch x86_64)
20    ELSEIF (CMAKE_SYSTEM_NAME STREQUAL "SunOS")
21      EXECUTE_PROCESS (COMMAND isainfo -k
22        COMMAND tr -d '\n'
23        OUTPUT_VARIABLE _arch)
24    ELSEIF (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
25      # If user didn't specify the arch via target_arch or
26      # CB_DOWNLOAD_DEPS_ARCH, assume that the target is the same as
27      # the current host architecture and derive that from
28      # Windows-provided environment variables.
29      IF (DEFINED ENV{PROCESSOR_ARCHITEW6432})
30        STRING (TOLOWER "$ENV{PROCESSOR_ARCHITEW6432}" _arch)
31      ELSE ()
32        STRING (TOLOWER "$ENV{PROCESSOR_ARCHITECTURE}" _arch)
33      ENDIF ()
34    ELSE (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
35      STRING (TOLOWER ${CMAKE_SYSTEM_PROCESSOR} _arch)
36    ENDIF (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
37    SET (CB_DOWNLOAD_DEPS_ARCH ${_arch} CACHE STRING
38      "Architecture for downloaded dependencies")
39    MARK_AS_ADVANCED (CB_DOWNLOAD_DEPS_ARCH)
40  ENDIF (DEFINED CB_DOWNLOAD_DEPS_ARCH)
41  SET (${var} ${_arch})
42ENDMACRO (_DETERMINE_ARCH)
43
44
45# Returns a lowercased version of a given lsb_release field.
46MACRO (_LSB_RELEASE field retval)
47  EXECUTE_PROCESS (COMMAND lsb_release "--${field}"
48  OUTPUT_VARIABLE _output ERROR_VARIABLE _output RESULT_VARIABLE _result)
49  IF (_result)
50    MESSAGE (FATAL_ERROR "Cannot determine Linux revision! Output from "
51    "lsb_release --${field}: ${_output}")
52  ENDIF (_result)
53  STRING (REGEX REPLACE "^[^:]*:" "" _output "${_output}")
54  STRING (TOLOWER "${_output}" _output)
55  STRING (STRIP "${_output}" ${retval})
56ENDMACRO (_LSB_RELEASE)
57
58
59# Returns a simple string describing the current platform. Possible
60# return values currently include: windows_msvc2017; windows_msvc2015;
61# windows_msvc2013; windows_msvc2012; macosx; or any value from
62# _DETERMINE_LINUX_DISTRO.
63MACRO (_DETERMINE_PLATFORM var)
64  IF (DEFINED CB_DOWNLOAD_DEPS_PLATFORM)
65    SET (_plat ${CB_DOWNLOAD_DEPS_PLATFORM})
66  ELSE (DEFINED CB_DOWNLOAD_DEPS_PLATFORM)
67    SET (_plat ${CMAKE_SYSTEM_NAME})
68    IF (_plat STREQUAL "Windows")
69	  if (${MSVC_VERSION} LESS 1800)
70	    SET (_plat "windows_msvc2012")
71	  elseif (${MSVC_VERSION} LESS 1900)
72	    SET (_plat "windows_msvc2013")
73	  elseif (${MSVC_VERSION} LESS 1910)
74	    SET (_plat "windows_msvc2015")
75	  elseif (${MSVC_VERSION} LESS 1920)
76	    SET (_plat "windows_msvc2017")
77	  ELSE()
78        MESSAGE(FATAL_ERROR "Unsupported MSVC version: ${MSVC_VERSION}")
79      ENDIF ()
80    ELSEIF (_plat STREQUAL "Darwin")
81      SET (_plat "macosx")
82    ELSEIF (_plat STREQUAL "Linux")
83      FIND_PROGRAM(LSB_RELEASE lsb_release)
84      IF (LSB_RELEASE)
85        _DETERMINE_LINUX_DISTRO (_plat)
86      ELSE (LSB_RELEASE)
87        MESSAGE (WARNING "Can't determine Linux platform without lsb_release")
88        SET (_plat "unknown")
89      ENDIF (LSB_RELEASE)
90    ELSEIF (_plat STREQUAL "SunOS")
91      SET (_plat "sunos")
92    ELSEIF (_plat STREQUAL "FreeBSD")
93      SET (_plat "freebsd")
94    ELSE (_plat STREQUAL "Windows")
95      MESSAGE (WARNING "Sorry, don't recognize your system ${_plat}. ")
96      SET (_plat "unknown")
97    ENDIF (_plat STREQUAL "Windows")
98    SET (CB_DOWNLOAD_DEPS_PLATFORM ${_plat} CACHE STRING
99      "Platform for downloaded dependencies")
100    MARK_AS_ADVANCED (CB_DOWNLOAD_DEPS_PLATFORM)
101  ENDIF (DEFINED CB_DOWNLOAD_DEPS_PLATFORM)
102  SET (${var} ${_plat})
103ENDMACRO (_DETERMINE_PLATFORM)
104
105
106# Returns a simple string describing the current Linux distribution
107# compatibility. Possible return values currently include:
108# ubuntu14.04, ubuntu12.04, ubuntu10.04, centos5, centos6, debian7, debian8.
109MACRO (_DETERMINE_LINUX_DISTRO _distro)
110  _LSB_RELEASE (id _id)
111  _LSB_RELEASE (release _rel)
112  IF (_id STREQUAL "linuxmint")
113    # Linux Mint is an Ubuntu derivative; estimate nearest Ubuntu equivalent
114    SET (_id "ubuntu")
115    IF (_rel VERSION_LESS 13)
116      SET (_rel 10.04)
117    ELSEIF (_rel VERSION_LESS 17)
118      SET (_rel 12.04)
119    ELSEIF (_rel VERSION_LESS 18)
120      SET (_rel 14.04)
121    ELSE ()
122      SET (_rel 16.04)
123    ENDIF ()
124  ELSEIF (_id STREQUAL "debian" OR _id STREQUAL "centos" )
125    # Just use the major version from the CentOS/Debian identifier - we don't
126    # need different builds for different minor versions.
127    STRING (REGEX MATCH "[0-9]+" _rel "${_rel}")
128  ELSEIF (_id STREQUAL "fedora" AND _rel VERSION_LESS 26)
129    SET (_id "centos")
130    SET (_rel "7")
131  ELSEIF (_id STREQUAL "opensuse project" OR _id STREQUAL "suse linux")
132    SET(_id "suse")
133  ENDIF (_id STREQUAL "linuxmint")
134  SET (${_distro} "${_id}${_rel}")
135ENDMACRO (_DETERMINE_LINUX_DISTRO)
136
137# Returns number of CPUs the system has. The value can be overwritten by the
138# CB_CPU_COUNT environment variable. If neither of these work, return some
139# (hopefully) reasonable default.
140MACRO (_DETERMINE_CPU_COUNT _var)
141  SET(_count 0)
142  IF (DEFINED $ENV{CB_CPU_COUNT})
143    SET(_count $ENV{CB_CPU_COUNT})
144  ENDIF (DEFINED $ENV{CB_CPU_COUNT})
145
146  IF (_count EQUAL 0)
147    ProcessorCount(_count)
148  ENDIF (_count EQUAL 0)
149
150  IF (_count EQUAL 0)
151    MESSAGE(WARNING "Couldn't determine number of CPUs to use. Using default.")
152    SET(_count 4)
153  ENDIF (_count EQUAL 0)
154
155  SET(${_var} ${_count})
156ENDMACRO (_DETERMINE_CPU_COUNT)
157
158# Sets _platform to the name of the current platform if it is a supported
159# platform, or a False value otherwise.
160# "Supported" means that we produce and distribute builds to
161# customers on that platform.
162# QQQ This list should come from manifest/product-config.json ultimately.
163# _platform is in the same format as _DETERMINE_PLATFORM().
164MACRO (CB_GET_SUPPORTED_PLATFORM _supported_platform)
165  SET (${_supported_platform} 0)
166
167  # First get the current platform
168  _DETERMINE_PLATFORM(_platform)
169
170  # .. and check it against the list, returning it if found.
171  SET (_supported_platforms
172       "centos6" "centos7"
173       "debian7" "debian8" "debian9"
174       "macosx"
175       "suse11.2" "suse12.2"
176       "ubuntu14.04" "ubuntu16.04"
177       "windows_msvc2015")
178  LIST (FIND _supported_platforms ${_platform} _index)
179  IF (_index GREATER "-1")
180    SET(${_supported_platform} ${_platform})
181  ENDIF (_index GREATER "-1")
182ENDMACRO (CB_GET_SUPPORTED_PLATFORM)
183