aboutsummaryrefslogtreecommitdiff
path: root/CMakeLists.txt
blob: 431e95b8e28a0b90d0b881fea04cbe30118e7754 (plain) (blame)
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
# PSn00bSDK main build script
# (C) 2021 spicyjpeg - MPL licensed

# NOTE: CMake doesn't support using multiple toolchains in a single project,
# so we can't use add_subdirectory() to build both the libraries and tools. A
# workaround is to use ExternalProject_Add() to launch multiple independent
# CMake instances, creating what's known as a "superbuild".

cmake_minimum_required(VERSION 3.21)
include(ExternalProject)

project(
	PSn00bSDK
	LANGUAGES    NONE
	# IMPORTANT TODO: set a version number
	VERSION      0.1.0
	DESCRIPTION  "Open source PlayStation 1 SDK"
	HOMEPAGE_URL "http://lameguy64.net/?page=psn00bsdk"
)

# Including this without initializing at least one language throws a warning and
# there's no way to mute it.
include(GNUInstallDirs)

## Settings

# These are passed through to libpsn00b and the examples (they are defined in
# the toolchain file).
set(
	PSN00BSDK_TC $ENV{PSN00BSDK_TC}
	CACHE PATH   "Path to the GCC toolchain's installation directory"
)
set(
	PSN00BSDK_TARGET mipsel-unknown-elf
	CACHE STRING     "GCC toolchain target triplet"
)

set(
	SKIP_TINYXML2 OFF
	CACHE BOOL    "Skip downloading and building tinyxml2 (if already installed)"
)
set(
	SKIP_MKPSXISO OFF
	CACHE BOOL    "Skip downloading and building mkpsxiso (if already installed)"
)
set(
	SKIP_EXAMPLES OFF
	CACHE BOOL    "Skip building SDK examples (not required for installation)"
)

# Forward some important variables to mkpsxiso and to the subprojects (they are
# not inherited automatically as they are not environment variables). This also
# sets all subprojects to "install" everything to a temporary directory in the
# build tree, so they don't actually get installed until "cmake --install" is
# invoked (ExternalProject_Add() runs the subprojects' install step at build
# time).
set(
	COMMON_ARGS
	-DPSN00BSDK_TC:PATH=${PSN00BSDK_TC}
	-DPSN00BSDK_TARGET:STRING=${PSN00BSDK_TARGET}
	-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
)
set(
	EXT_LIBRARY_ARGS
	-DCMAKE_TOOLCHAIN_FILE:FILEPATH=${CMAKE_TOOLCHAIN_FILE}
	-DCMAKE_INSTALL_PREFIX:PATH=${PROJECT_BINARY_DIR}/install_temp
	-DCMAKE_MSVC_RUNTIME_LIBRARY:STRING=MultiThreaded$<$<CONFIG:Debug>:Debug>
)
set(
	SUBPROJECT_ARGS
	-DCMAKE_TOOLCHAIN_FILE:FILEPATH=${CMAKE_TOOLCHAIN_FILE}
	-DCMAKE_INSTALL_PREFIX:PATH=${PROJECT_BINARY_DIR}/install_tree
)
set(
	EXAMPLES_ARGS
	-DCMAKE_TOOLCHAIN_FILE:FILEPATH=${PROJECT_BINARY_DIR}/install_tree/${CMAKE_INSTALL_LIBDIR}/libpsn00b/cmake/sdk.cmake
	-DCMAKE_INSTALL_PREFIX:PATH=${PROJECT_BINARY_DIR}/examples
)

## External dependencies

if(NOT SKIP_TINYXML2)
	list(APPEND SUBPROJECT_ARGS -Dtinyxml2_ROOT:PATH=${PROJECT_BINARY_DIR}/install_temp)

	ExternalProject_Add(
		tinyxml2
		GIT_REPOSITORY   "https://github.com/leethomason/tinyxml2"
		CMAKE_CACHE_ARGS ${COMMON_ARGS} ${EXT_LIBRARY_ARGS}
		INSTALL_DIR      install_temp
	)
else()
	list(APPEND SUBPROJECT_ARGS -Dtinyxml2_ROOT:PATH=${tinyxml2_ROOT})

	# Create a dummy target so CMake doesn't throw missing dependency errors.
	add_library(tinyxml2 INTERFACE)
endif()

if(NOT SKIP_MKPSXISO)
	ExternalProject_Add(
		mkpsxiso
		GIT_REPOSITORY   "https://github.com/Lameguy64/mkpsxiso"
		CMAKE_CACHE_ARGS ${COMMON_ARGS} ${SUBPROJECT_ARGS}
		INSTALL_DIR      install_tree
		DEPENDS          tinyxml2
	)
else()
	add_library(mkpsxiso INTERFACE)
endif()

## Subprojects

ExternalProject_Add(
	libpsn00b
	SOURCE_DIR       ${PROJECT_SOURCE_DIR}/libpsn00b
	CMAKE_CACHE_ARGS ${COMMON_ARGS} ${SUBPROJECT_ARGS}
	INSTALL_DIR      install_tree
)
ExternalProject_Add(
	tools
	SOURCE_DIR       ${PROJECT_SOURCE_DIR}/tools
	CMAKE_CACHE_ARGS ${COMMON_ARGS} ${SUBPROJECT_ARGS}
	INSTALL_DIR      install_tree
	DEPENDS          tinyxml2
)
ExternalProject_Add(
	examples
	SOURCE_DIR       ${PROJECT_SOURCE_DIR}/examples
	CMAKE_CACHE_ARGS ${COMMON_ARGS} ${EXAMPLES_ARGS}
	INSTALL_DIR      examples
	DEPENDS          libpsn00b tools mkpsxiso
	EXCLUDE_FROM_ALL ${SKIP_EXAMPLES}
)

# Install all files in the temporary installation tree, as well as static files
# from the source tree, when "cmake --install" is invoked.
install(
	DIRECTORY   ${PROJECT_BINARY_DIR}/install_tree/ # THE TRAILING SLASH IS IMPORTANT
	DESTINATION .
	COMPONENT   sdk
	USE_SOURCE_PERMISSIONS
)
install(
	DIRECTORY   doc template
	DESTINATION ${CMAKE_INSTALL_DATADIR}/psn00bsdk
	COMPONENT   docs
)

## CPack configuration

include(cpack/setup.cmake)