xref: /aosp_15_r20/external/libgav1/cmake/libgav1_helpers.cmake (revision 095378508e87ed692bf8dfeb34008b65b3735891)
1*09537850SAkhilesh Sanikop# Copyright 2019 The libgav1 Authors
2*09537850SAkhilesh Sanikop#
3*09537850SAkhilesh Sanikop# Licensed under the Apache License, Version 2.0 (the "License");
4*09537850SAkhilesh Sanikop# you may not use this file except in compliance with the License.
5*09537850SAkhilesh Sanikop# You may obtain a copy of the License at
6*09537850SAkhilesh Sanikop#
7*09537850SAkhilesh Sanikop#      http://www.apache.org/licenses/LICENSE-2.0
8*09537850SAkhilesh Sanikop#
9*09537850SAkhilesh Sanikop# Unless required by applicable law or agreed to in writing, software
10*09537850SAkhilesh Sanikop# distributed under the License is distributed on an "AS IS" BASIS,
11*09537850SAkhilesh Sanikop# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*09537850SAkhilesh Sanikop# See the License for the specific language governing permissions and
13*09537850SAkhilesh Sanikop# limitations under the License.
14*09537850SAkhilesh Sanikop
15*09537850SAkhilesh Sanikopif(LIBGAV1_CMAKE_LIBGAV1_HELPERS_CMAKE_)
16*09537850SAkhilesh Sanikop  return()
17*09537850SAkhilesh Sanikopendif() # LIBGAV1_CMAKE_LIBGAV1_HELPERS_CMAKE_
18*09537850SAkhilesh Sanikopset(LIBGAV1_CMAKE_LIBGAV1_HELPERS_CMAKE_ 1)
19*09537850SAkhilesh Sanikop
20*09537850SAkhilesh Sanikop# Kills build generation using message(FATAL_ERROR) and outputs all data passed
21*09537850SAkhilesh Sanikop# to the console via use of $ARGN.
22*09537850SAkhilesh Sanikopmacro(libgav1_die)
23*09537850SAkhilesh Sanikop  # macro parameters are not variables so a temporary is needed to work with
24*09537850SAkhilesh Sanikop  # list().
25*09537850SAkhilesh Sanikop  set(msg ${ARGN})
26*09537850SAkhilesh Sanikop  # message(${ARGN}) will merge all list elements with no separator while
27*09537850SAkhilesh Sanikop  # "${ARGN}" will output the list as a ';' delimited string.
28*09537850SAkhilesh Sanikop  list(JOIN msg " " msg)
29*09537850SAkhilesh Sanikop  message(FATAL_ERROR "${msg}")
30*09537850SAkhilesh Sanikopendmacro()
31*09537850SAkhilesh Sanikop
32*09537850SAkhilesh Sanikop# Converts semi-colon delimited list variable(s) to string. Output is written to
33*09537850SAkhilesh Sanikop# variable supplied via the DEST parameter. Input is from an expanded variable
34*09537850SAkhilesh Sanikop# referenced by SOURCE and/or variable(s) referenced by SOURCE_VARS.
35*09537850SAkhilesh Sanikopmacro(libgav1_set_and_stringify)
36*09537850SAkhilesh Sanikop  set(optional_args)
37*09537850SAkhilesh Sanikop  set(single_value_args DEST SOURCE_VAR)
38*09537850SAkhilesh Sanikop  set(multi_value_args SOURCE SOURCE_VARS)
39*09537850SAkhilesh Sanikop  cmake_parse_arguments(sas "${optional_args}" "${single_value_args}"
40*09537850SAkhilesh Sanikop                        "${multi_value_args}" ${ARGN})
41*09537850SAkhilesh Sanikop
42*09537850SAkhilesh Sanikop  if(NOT sas_DEST OR NOT (sas_SOURCE OR sas_SOURCE_VARS))
43*09537850SAkhilesh Sanikop    libgav1_die("libgav1_set_and_stringify: DEST and at least one of SOURCE "
44*09537850SAkhilesh Sanikop                "SOURCE_VARS required.")
45*09537850SAkhilesh Sanikop  endif()
46*09537850SAkhilesh Sanikop
47*09537850SAkhilesh Sanikop  unset(${sas_DEST})
48*09537850SAkhilesh Sanikop
49*09537850SAkhilesh Sanikop  if(sas_SOURCE)
50*09537850SAkhilesh Sanikop    # $sas_SOURCE is one or more expanded variables, just copy the values to
51*09537850SAkhilesh Sanikop    # $sas_DEST.
52*09537850SAkhilesh Sanikop    set(${sas_DEST} "${sas_SOURCE}")
53*09537850SAkhilesh Sanikop  endif()
54*09537850SAkhilesh Sanikop
55*09537850SAkhilesh Sanikop  if(sas_SOURCE_VARS)
56*09537850SAkhilesh Sanikop    # $sas_SOURCE_VARS is one or more variable names. Each iteration expands a
57*09537850SAkhilesh Sanikop    # variable and appends it to $sas_DEST.
58*09537850SAkhilesh Sanikop    foreach(source_var ${sas_SOURCE_VARS})
59*09537850SAkhilesh Sanikop      set(${sas_DEST} "${${sas_DEST}} ${${source_var}}")
60*09537850SAkhilesh Sanikop    endforeach()
61*09537850SAkhilesh Sanikop
62*09537850SAkhilesh Sanikop    # Because $sas_DEST can be empty when entering this scope leading whitespace
63*09537850SAkhilesh Sanikop    # can be introduced to $sas_DEST on the first iteration of the above loop.
64*09537850SAkhilesh Sanikop    # Remove it:
65*09537850SAkhilesh Sanikop    string(STRIP "${${sas_DEST}}" ${sas_DEST})
66*09537850SAkhilesh Sanikop  endif()
67*09537850SAkhilesh Sanikop
68*09537850SAkhilesh Sanikop  # Lists in CMake are simply semicolon delimited strings, so stringification is
69*09537850SAkhilesh Sanikop  # just a find and replace of the semicolon.
70*09537850SAkhilesh Sanikop  string(REPLACE ";" " " ${sas_DEST} "${${sas_DEST}}")
71*09537850SAkhilesh Sanikop
72*09537850SAkhilesh Sanikop  if(LIBGAV1_VERBOSE GREATER 1)
73*09537850SAkhilesh Sanikop    message("libgav1_set_and_stringify: ${sas_DEST}=${${sas_DEST}}")
74*09537850SAkhilesh Sanikop  endif()
75*09537850SAkhilesh Sanikopendmacro()
76*09537850SAkhilesh Sanikop
77*09537850SAkhilesh Sanikop# Creates a dummy source file in $LIBGAV1_GENERATED_SOURCES_DIRECTORY and adds
78*09537850SAkhilesh Sanikop# it to the specified target. Optionally adds its path to a list variable.
79*09537850SAkhilesh Sanikop#
80*09537850SAkhilesh Sanikop# libgav1_create_dummy_source_file(<TARGET <target> BASENAME <basename of file>>
81*09537850SAkhilesh Sanikop# [LISTVAR <list variable>])
82*09537850SAkhilesh Sanikopmacro(libgav1_create_dummy_source_file)
83*09537850SAkhilesh Sanikop  set(optional_args)
84*09537850SAkhilesh Sanikop  set(single_value_args TARGET BASENAME LISTVAR)
85*09537850SAkhilesh Sanikop  set(multi_value_args)
86*09537850SAkhilesh Sanikop  cmake_parse_arguments(cdsf "${optional_args}" "${single_value_args}"
87*09537850SAkhilesh Sanikop                        "${multi_value_args}" ${ARGN})
88*09537850SAkhilesh Sanikop
89*09537850SAkhilesh Sanikop  if(NOT cdsf_TARGET OR NOT cdsf_BASENAME)
90*09537850SAkhilesh Sanikop    libgav1_die(
91*09537850SAkhilesh Sanikop      "libgav1_create_dummy_source_file: TARGET and BASENAME required.")
92*09537850SAkhilesh Sanikop  endif()
93*09537850SAkhilesh Sanikop
94*09537850SAkhilesh Sanikop  if(NOT LIBGAV1_GENERATED_SOURCES_DIRECTORY)
95*09537850SAkhilesh Sanikop    set(LIBGAV1_GENERATED_SOURCES_DIRECTORY "${libgav1_build}/gen_src")
96*09537850SAkhilesh Sanikop  endif()
97*09537850SAkhilesh Sanikop
98*09537850SAkhilesh Sanikop  set(dummy_source_dir "${LIBGAV1_GENERATED_SOURCES_DIRECTORY}")
99*09537850SAkhilesh Sanikop  set(dummy_source_file
100*09537850SAkhilesh Sanikop      "${dummy_source_dir}/libgav1_${cdsf_TARGET}_${cdsf_BASENAME}.cc")
101*09537850SAkhilesh Sanikop  set(dummy_source_code
102*09537850SAkhilesh Sanikop      "// Generated file. DO NOT EDIT!\n"
103*09537850SAkhilesh Sanikop      "// C++ source file created for target ${cdsf_TARGET}.\n"
104*09537850SAkhilesh Sanikop      "void libgav1_${cdsf_TARGET}_${cdsf_BASENAME}_dummy_function(void)\;\n"
105*09537850SAkhilesh Sanikop      "void libgav1_${cdsf_TARGET}_${cdsf_BASENAME}_dummy_function(void) {}\n")
106*09537850SAkhilesh Sanikop  file(WRITE "${dummy_source_file}" ${dummy_source_code})
107*09537850SAkhilesh Sanikop
108*09537850SAkhilesh Sanikop  target_sources(${cdsf_TARGET} PRIVATE ${dummy_source_file})
109*09537850SAkhilesh Sanikop
110*09537850SAkhilesh Sanikop  if(cdsf_LISTVAR)
111*09537850SAkhilesh Sanikop    list(APPEND ${cdsf_LISTVAR} "${dummy_source_file}")
112*09537850SAkhilesh Sanikop  endif()
113*09537850SAkhilesh Sanikopendmacro()
114*09537850SAkhilesh Sanikop
115*09537850SAkhilesh Sanikop# Loads the version components from $libgav1_source/gav1/version.h and sets the
116*09537850SAkhilesh Sanikop# corresponding CMake variables:
117*09537850SAkhilesh Sanikop# - LIBGAV1_MAJOR_VERSION
118*09537850SAkhilesh Sanikop# - LIBGAV1_MINOR_VERSION
119*09537850SAkhilesh Sanikop# - LIBGAV1_PATCH_VERSION
120*09537850SAkhilesh Sanikop# - LIBGAV1_VERSION, which is:
121*09537850SAkhilesh Sanikop#   - $LIBGAV1_MAJOR_VERSION.$LIBGAV1_MINOR_VERSION.$LIBGAV1_PATCH_VERSION
122*09537850SAkhilesh Sanikopmacro(libgav1_load_version_info)
123*09537850SAkhilesh Sanikop  file(STRINGS "${libgav1_source}/gav1/version.h" version_file_strings)
124*09537850SAkhilesh Sanikop  foreach(str ${version_file_strings})
125*09537850SAkhilesh Sanikop    if(str MATCHES "#define LIBGAV1_")
126*09537850SAkhilesh Sanikop      if(str MATCHES "#define LIBGAV1_MAJOR_VERSION ")
127*09537850SAkhilesh Sanikop        string(REPLACE "#define LIBGAV1_MAJOR_VERSION " "" LIBGAV1_MAJOR_VERSION
128*09537850SAkhilesh Sanikop                       "${str}")
129*09537850SAkhilesh Sanikop      elseif(str MATCHES "#define LIBGAV1_MINOR_VERSION ")
130*09537850SAkhilesh Sanikop        string(REPLACE "#define LIBGAV1_MINOR_VERSION " "" LIBGAV1_MINOR_VERSION
131*09537850SAkhilesh Sanikop                       "${str}")
132*09537850SAkhilesh Sanikop      elseif(str MATCHES "#define LIBGAV1_PATCH_VERSION ")
133*09537850SAkhilesh Sanikop        string(REPLACE "#define LIBGAV1_PATCH_VERSION " "" LIBGAV1_PATCH_VERSION
134*09537850SAkhilesh Sanikop                       "${str}")
135*09537850SAkhilesh Sanikop      endif()
136*09537850SAkhilesh Sanikop    endif()
137*09537850SAkhilesh Sanikop  endforeach()
138*09537850SAkhilesh Sanikop  set(LIBGAV1_VERSION "${LIBGAV1_MAJOR_VERSION}.${LIBGAV1_MINOR_VERSION}")
139*09537850SAkhilesh Sanikop  set(LIBGAV1_VERSION "${LIBGAV1_VERSION}.${LIBGAV1_PATCH_VERSION}")
140*09537850SAkhilesh Sanikopendmacro()
141