blob: 4a5df37b2d6c0c6c64a1a677e6c38842aa42001a [file] [log] [blame]
Gyorgy Szing49091802020-11-24 00:33:09 +01001#-------------------------------------------------------------------------------
2# Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
3#
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
118 ${SP_DEV_KIT_SRC_DIR}/sp_trace.c
119 )
120
121 target_include_directories(sp_header
122 PRIVATE
123 ${SP_DEV_KIT_INC_DIR}
124 )
125
126 target_link_libraries(sp_header PRIVATE spdevkit::libutils)
127 add_library(spdevkit::sp_header ALIAS sp_header)
128 list(APPEND SP_DEV_KIT_LIBRARIES spdevkit::sp_header)
129endif()
130
131#[===[.rst:
132.. cmake:command:: sp_dev_kit_configure_linking
133
134 .. code-block:: cmake
135
136 sp_dev_kit_configure_linking(TARGET <executable target> DEFINES <list of pre-processor macros>)
137
138 Connect an executable target to SP DEV KIT link requirements:
139 - add rules to run the linker command file trough the pre-processor if needed
140 - configure the target to use the linker command file
141 - link SP DEV KIT libraries to the target
142
143 Inputs:
144
145 ``TARGET``
146 Mandatory. The name of an already defined executable target (add_executable())
147
148 ``DEFINES``
149 Optional. Macro definitions for the pre-processing step.
150
151#]===]
152function(sp_dev_kit_configure_linking)
153 set(options )
154 set(oneValueArgs TARGET)
155 set(multiValueArgs DEFINES)
156 cmake_parse_arguments(MY_PARAMS "${options}" "${oneValueArgs}"
157 "${multiValueArgs}" ${ARGN} )
158
159 if(NOT DEFINED MY_PARAMS_TARGET)
160 message(FATAL_ERROR "sp_dev_kit_configure_linking: mandatory parameter TARGET not defined!")
161 endif()
162
163 if(NOT DEFINED MY_PARAMS_DEFINES)
164 set(MY_PARAMS_DEFINES "")
165 endif()
166
167 compiler_preprocess_file(
168 SRC ${SP_DEV_KIT_DIR}/src/sp.ld.S
169 DST ${CMAKE_BINARY_DIR}/sp.ld
170 DEFINES ${MY_PARAMS_DEFINES}
171 )
172
173 add_custom_target(${MY_PARAMS_TARGET}-pplscript DEPENDS ${CMAKE_BINARY_DIR}/sp.ld)
174 add_dependencies(${MY_PARAMS_TARGET} ${MY_PARAMS_TARGET}-pplscript)
175
176 target_link_options(${MY_PARAMS_TARGET} PRIVATE
177 -T${CMAKE_BINARY_DIR}/sp.ld
178 )
179endfunction()
180