blob: dc81cbc38ad1959c9ee3c0eb407cefe53c734d3e [file] [log] [blame]
Gyorgy Szing49091802020-11-24 00:33:09 +01001#-------------------------------------------------------------------------------
Imre Kis2ccd8e82021-10-08 11:21:14 +02002# Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
Gyorgy Szing49091802020-11-24 00:33:09 +01003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8#[===[.rst:
9 Abstraction to interface to SP DEV KIT.
10 ---------------------------------------
11
12 SP DEV KIT is a component generated by the OP-TEE OS build and defines the
13 FF-A SP interface for the OS to SPs.
14
15 This file provides the following functionality:
16 - creates a static library to easy building the SP side component of the
17 SP DEV KIT. This library will be properly configured with transitive
18 dependencies to apply the needed compiler configurations to the
19 target linking against it.
20 - defines a function to allow configuration of the linking phase of the
21 executable target. (See: :command:`sp_dev_kit_configure_linking`)
22
23 Inputs:
24 ``SP_DEV_KIT_DIR``
25 A cmake or environment variable specifying the location of the SP DEV KIT.
26 This is mandatory.
27
28 ``SP_DEV_KIT_INC_DIR``
29 The SP DEV KIT depends on some header files living in the SP. This variable
30 must be set to an include path giving access to these headers.
31
32 Outputs:
33 :command:sp_dev_kit_configure_linking()
34
35 :variable:`SP_DEV_KIT_LIBRARIES`
36
37#]===]
38
39# Store SP DEV KIT location to cache.
40# If a cmake variable exist, use it as is.
41# If not, try to copy over the value from the environment.
42if(NOT DEFINED SP_DEV_KIT_DIR AND NOT DEFINED ENV{SP_DEV_KIT_DIR})
43 message(FATAL_ERROR "'SP_DEV_KIT_DIR' is not defined.")
44endif()
45set(SP_DEV_KIT_DIR $ENV{SP_DEV_KIT_DIR} CACHE STRING "SP dev kit from original OP-TEE build system")
46
47if (DEFINED ENV{SP_DEV_KIT_DIR} AND NOT SP_DEV_KIT_DIR STREQUAL "$ENV{SP_DEV_KIT_DIR}")
48 message(WARNING "Suspicious settings: the value of SP_DEV_KIT_DIR in the environment is not matching cmakes settings!")
49endif()
50
51# Find the directories inside SP DEV KIT. This gives more flexibility than when using static settings.
52find_path (SP_DEV_KIT_SRC_DIR
53 NAMES
54 sp_header.c
55 PATHS
56 ${SP_DEV_KIT_DIR}
57 PATH_SUFFIXES
58 "src"
59 NO_DEFAULT_PATH
60 REQUIRED
61 DOC
62 "SP DEV KIT source directory"
63 )
64
65find_path (SP_DEV_KIT_INCLUDE_DIR
66 NAMES
67 ffa.h atomic.h compiler.h
68 PATHS
69 ${SP_DEV_KIT_DIR}
70 PATH_SUFFIXES
71 "include"
72 NO_DEFAULT_PATH
73 REQUIRED
74 DOC
75 "SP DEV KIT include directory"
76 )
77
78find_path (SP_DEV_KIT_LIB_DIR
79 NAMES
80 ${CMAKE_STATIC_LIBRARY_PREFIX}utils${CMAKE_STATIC_LIBRARY_SUFFIX} libutils.a utils.a utils.lib libutils.link_libraries
81 PATHS
82 ${SP_DEV_KIT_DIR}
83 PATH_SUFFIXES
84 "lib"
85 NO_DEFAULT_PATH
86 REQUIRED
87 DOC
88 "SP DEV KIT library directory"
89 )
90
91#[===[.rst:
92.. cmake:variable:: SP_DEV_KIT_LIBRARIES
93
94 List of libraries forming the SP DEV KIT interface.
95
96#]===]
97
98# Create an imported target for libutils.a
99add_library(spdevkit::libutils STATIC IMPORTED)
100set_target_properties(spdevkit::libutils PROPERTIES
101 INTERFACE_INCLUDE_DIRECTORIES "${SP_DEV_KIT_INCLUDE_DIR}"
102 INTERFACE_LINK_LIBRARIES "${SP_DEV_KIT_LIB_DIR}/libutils.a"
103 IMPORTED_LOCATION "${SP_DEV_KIT_LIB_DIR}/libutils.a"
104)
105list(APPEND SP_DEV_KIT_LIBRARIES spdevkit::libutils)
106
107if (NOT Spdevkit_FIND_COMPONENTS OR "SP_HEADER" IN_LIST Spdevkit_FIND_COMPONENTS)
108 if (NOT DEFINED SP_DEV_KIT_INC_DIR)
109 message(FATAL_ERROR "Mandatory input variable 'SP_DEV_KIT_INC_DIR' is not defined.")
110 endif()
111
112 # Define a static library to compile the SP side source files and to
113 # capture dependencies (settings).
114 add_library(sp_header STATIC
115 ${SP_DEV_KIT_SRC_DIR}/sp_assert.c
116 ${SP_DEV_KIT_SRC_DIR}/sp_entry.c
117 ${SP_DEV_KIT_SRC_DIR}/sp_header.c
Gyorgy Szing49091802020-11-24 00:33:09 +0100118 )
119
120 target_include_directories(sp_header
121 PRIVATE
122 ${SP_DEV_KIT_INC_DIR}
123 )
124
125 target_link_libraries(sp_header PRIVATE spdevkit::libutils)
126 add_library(spdevkit::sp_header ALIAS sp_header)
127 list(APPEND SP_DEV_KIT_LIBRARIES spdevkit::sp_header)
128endif()
129
130#[===[.rst:
131.. cmake:command:: sp_dev_kit_configure_linking
132
133 .. code-block:: cmake
134
135 sp_dev_kit_configure_linking(TARGET <executable target> DEFINES <list of pre-processor macros>)
136
137 Connect an executable target to SP DEV KIT link requirements:
138 - add rules to run the linker command file trough the pre-processor if needed
139 - configure the target to use the linker command file
140 - link SP DEV KIT libraries to the target
141
142 Inputs:
143
144 ``TARGET``
145 Mandatory. The name of an already defined executable target (add_executable())
146
147 ``DEFINES``
148 Optional. Macro definitions for the pre-processing step.
149
150#]===]
151function(sp_dev_kit_configure_linking)
152 set(options )
153 set(oneValueArgs TARGET)
154 set(multiValueArgs DEFINES)
155 cmake_parse_arguments(MY_PARAMS "${options}" "${oneValueArgs}"
156 "${multiValueArgs}" ${ARGN} )
157
158 if(NOT DEFINED MY_PARAMS_TARGET)
159 message(FATAL_ERROR "sp_dev_kit_configure_linking: mandatory parameter TARGET not defined!")
160 endif()
161
162 if(NOT DEFINED MY_PARAMS_DEFINES)
163 set(MY_PARAMS_DEFINES "")
164 endif()
165
166 compiler_preprocess_file(
167 SRC ${SP_DEV_KIT_DIR}/src/sp.ld.S
168 DST ${CMAKE_BINARY_DIR}/sp.ld
169 DEFINES ${MY_PARAMS_DEFINES}
170 )
171
172 add_custom_target(${MY_PARAMS_TARGET}-pplscript DEPENDS ${CMAKE_BINARY_DIR}/sp.ld)
173 add_dependencies(${MY_PARAMS_TARGET} ${MY_PARAMS_TARGET}-pplscript)
174
175 target_link_options(${MY_PARAMS_TARGET} PRIVATE
176 -T${CMAKE_BINARY_DIR}/sp.ld
177 )
178endfunction()
179