diff mbox series

[v12,05/11] support/misc/toolchainfile.cmake.in: allow variables to be overridden

Message ID 20220322232224.2842266-5-james.hilliard1@gmail.com
State Accepted
Headers show
Series [v12,01/11] package/llvm: bump to version 11.1.0 | expand

Commit Message

James Hilliard March 22, 2022, 11:22 p.m. UTC
Some packages such as libclc need to override cmake toolchain
variables, to avoid errors caused by trying to set overriden
variables ensure that they are not defined before being set.

This prevents difficult to debug silent dropping of overriden
variables.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 support/misc/toolchainfile.cmake.in | 108 +++++++++++++++++++++-------
 1 file changed, 81 insertions(+), 27 deletions(-)

Comments

Romain Naour March 31, 2022, 8:38 p.m. UTC | #1
Hello James,

Le 23/03/2022 à 00:22, James Hilliard a écrit :
> Some packages such as libclc need to override cmake toolchain
> variables, to avoid errors caused by trying to set overriden
> variables ensure that they are not defined before being set.
> 
> This prevents difficult to debug silent dropping of overriden
> variables.

Actually, it seems using the toolchainfile.cmake itself should be avoided for
such case.
For now only libclc package seems requires to override cmake toolchain variable,
I'm not sure about potential side effect of this change.

Best regards,
Romain

> 
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
>  support/misc/toolchainfile.cmake.in | 108 +++++++++++++++++++++-------
>  1 file changed, 81 insertions(+), 27 deletions(-)
> 
> diff --git a/support/misc/toolchainfile.cmake.in b/support/misc/toolchainfile.cmake.in
> index be575566a4..30c0e63bf2 100644
> --- a/support/misc/toolchainfile.cmake.in
> +++ b/support/misc/toolchainfile.cmake.in
> @@ -14,9 +14,15 @@ string(REPLACE "/share/buildroot" "" RELOCATED_HOST_DIR ${CMAKE_CURRENT_LIST_DIR
>  # so that it can find our custom platform description.
>  list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
>  
> -set(CMAKE_SYSTEM_NAME Buildroot)
> -set(CMAKE_SYSTEM_VERSION 1)
> -set(CMAKE_SYSTEM_PROCESSOR @@CMAKE_SYSTEM_PROCESSOR@@)
> +if(NOT DEFINED CMAKE_SYSTEM_NAME)
> +  set(CMAKE_SYSTEM_NAME Buildroot)
> +endif()
> +if(NOT DEFINED CMAKE_SYSTEM_VERSION)
> +  set(CMAKE_SYSTEM_VERSION 1)
> +endif()
> +if(NOT DEFINED CMAKE_SYSTEM_PROCESSOR)
> +  set(CMAKE_SYSTEM_PROCESSOR @@CMAKE_SYSTEM_PROCESSOR@@)
> +endif()
>  
>  # Set the {C,CXX}FLAGS appended by CMake depending on the build type
>  # defined by Buildroot. CMake defaults these variables with -g and/or
> @@ -28,11 +34,17 @@ set(CMAKE_SYSTEM_PROCESSOR @@CMAKE_SYSTEM_PROCESSOR@@)
>  # Note:
>  #   if the project forces some of these flag variables, Buildroot is
>  #   screwed up and there is nothing Buildroot can do about that :(
> -set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "Debug CFLAGS")
> -set(CMAKE_C_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release CFLAGS")
> +if(NOT DEFINED CMAKE_C_FLAGS_DEBUG)
> +  set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "Debug CFLAGS")
> +endif()
> +if(NOT DEFINED CMAKE_C_FLAGS_RELEASE)
> +  set(CMAKE_C_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release CFLAGS")
> +endif()
>  
>  # Build type from the Buildroot configuration
> -set(CMAKE_BUILD_TYPE @@CMAKE_BUILD_TYPE@@ CACHE STRING "Buildroot build configuration")
> +if(NOT DEFINED CMAKE_BUILD_TYPE)
> +  set(CMAKE_BUILD_TYPE @@CMAKE_BUILD_TYPE@@ CACHE STRING "Buildroot build configuration")
> +endif()
>  
>  # Buildroot defaults flags.
>  # If you are using this toolchainfile.cmake file outside of Buildroot and
> @@ -41,33 +53,75 @@ set(CMAKE_BUILD_TYPE @@CMAKE_BUILD_TYPE@@ CACHE STRING "Buildroot build configur
>  #     cmake -DCMAKE_C_FLAGS="@@TARGET_CFLAGS@@ -Dsome_custom_flag" ...
>  # * and make sure the project's CMake code extends them like this if needed:
>  #     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Dsome_definitions")
> -set(CMAKE_C_FLAGS "@@TARGET_CFLAGS@@" CACHE STRING "Buildroot CFLAGS")
> -set(CMAKE_EXE_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for executables")
> -set(CMAKE_SHARED_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for shared libraries")
> -set(CMAKE_MODULE_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for module libraries")
> +if(NOT DEFINED CMAKE_C_FLAGS)
> +  set(CMAKE_C_FLAGS "@@TARGET_CFLAGS@@" CACHE STRING "Buildroot CFLAGS")
> +endif()
> +if(NOT DEFINED CMAKE_EXE_LINKER_FLAGS)
> +  set(CMAKE_EXE_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for executables")
> +endif()
> +if(NOT DEFINED CMAKE_SHARED_LINKER_FLAGS)
> +  set(CMAKE_SHARED_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for shared libraries")
> +endif()
> +if(NOT DEFINED CMAKE_MODULE_LINKER_FLAGS)
> +  set(CMAKE_MODULE_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for module libraries")
> +endif()
>  
> -set(CMAKE_INSTALL_SO_NO_EXE 0)
> +if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)
> +  set(CMAKE_INSTALL_SO_NO_EXE 0)
> +endif()
>  
> -set(CMAKE_PROGRAM_PATH "${RELOCATED_HOST_DIR}/bin")
> -set(CMAKE_SYSROOT "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
> -set(CMAKE_FIND_ROOT_PATH "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
> -set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
> -set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
> -set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
> -set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
> +if(NOT DEFINED CMAKE_PROGRAM_PATH)
> +  set(CMAKE_PROGRAM_PATH "${RELOCATED_HOST_DIR}/bin")
> +endif()
> +if(NOT DEFINED CMAKE_SYSROOT)
> +  set(CMAKE_SYSROOT "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
> +endif()
> +if(NOT DEFINED CMAKE_FIND_ROOT_PATH)
> +  set(CMAKE_FIND_ROOT_PATH "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
> +endif()
> +if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PROGRAM)
> +  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
> +endif()
> +if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PACKAGE)
> +  set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
> +endif()
> +if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
> +  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
> +endif()
> +if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)
> +  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
> +endif()
>  set(ENV{PKG_CONFIG_SYSROOT_DIR} "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
>  
>  # This toolchain file can be used both inside and outside Buildroot.
> -set(CMAKE_C_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_CC@@")
> +if(NOT DEFINED CMAKE_C_COMPILER)
> +  set(CMAKE_C_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_CC@@")
> +endif()
>  if(@@TOOLCHAIN_HAS_CXX@@)
> -  set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "Debug CXXFLAGS")
> -  set(CMAKE_CXX_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release CXXFLAGS")
> -  set(CMAKE_CXX_FLAGS "@@TARGET_CXXFLAGS@@" CACHE STRING "Buildroot CXXFLAGS")
> -  set(CMAKE_CXX_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_CXX@@")
> +  if(NOT DEFINED CMAKE_CXX_FLAGS_DEBUG)
> +    set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "Debug CXXFLAGS")
> +  endif()
> +  if(NOT DEFINED CMAKE_CXX_FLAGS_RELEASE)
> +    set(CMAKE_CXX_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release CXXFLAGS")
> +  endif()
> +  if(NOT DEFINED CMAKE_CXX_FLAGS)
> +    set(CMAKE_CXX_FLAGS "@@TARGET_CXXFLAGS@@" CACHE STRING "Buildroot CXXFLAGS")
> +  endif()
> +  if(NOT DEFINED CMAKE_CXX_COMPILER)
> +    set(CMAKE_CXX_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_CXX@@")
> +  endif()
>  endif()
>  if(@@TOOLCHAIN_HAS_FORTRAN@@)
> -  set(CMAKE_Fortran_FLAGS_DEBUG "" CACHE STRING "Debug Fortran FLAGS")
> -  set(CMAKE_Fortran_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release Fortran FLAGS")
> -  set(CMAKE_Fortran_FLAGS "@@TARGET_FCFLAGS@@" CACHE STRING "Buildroot FCFLAGS")
> -  set(CMAKE_Fortran_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_FC@@")
> +  if(NOT DEFINED CMAKE_Fortran_FLAGS_DEBUG)
> +    set(CMAKE_Fortran_FLAGS_DEBUG "" CACHE STRING "Debug Fortran FLAGS")
> +  endif()
> +  if(NOT DEFINED CMAKE_Fortran_FLAGS_RELEASE)
> +    set(CMAKE_Fortran_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release Fortran FLAGS")
> +  endif()
> +  if(NOT DEFINED CMAKE_Fortran_FLAGS)
> +    set(CMAKE_Fortran_FLAGS "@@TARGET_FCFLAGS@@" CACHE STRING "Buildroot FCFLAGS")
> +  endif()
> +  if(NOT DEFINED CMAKE_Fortran_COMPILER)
> +    set(CMAKE_Fortran_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_FC@@")
> +  endif()
>  endif()
James Hilliard March 31, 2022, 9:29 p.m. UTC | #2
On Thu, Mar 31, 2022 at 2:38 PM Romain Naour <romain.naour@gmail.com> wrote:
>
> Hello James,
>
> Le 23/03/2022 à 00:22, James Hilliard a écrit :
> > Some packages such as libclc need to override cmake toolchain
> > variables, to avoid errors caused by trying to set overriden
> > variables ensure that they are not defined before being set.
> >
> > This prevents difficult to debug silent dropping of overriden
> > variables.
>
> Actually, it seems using the toolchainfile.cmake itself should be avoided for
> such case.

Not really IMO, we only want to override some toolchain variables in most
cases, rather than all of them, this makes that easy to do.

It also prevents difficult to debug errors since accidentally
overriding a toolchain
variable seems to cause other toolchain variables to fail to apply
properly otherwise.

> For now only libclc package seems requires to override cmake toolchain variable,
> I'm not sure about potential side effect of this change.

There's another pending series that this helps, see discussion:
https://lore.kernel.org/buildroot/057A8C3C-8DAC-46A3-86AB-2DAA92A9368B@gmail.com/

Since overriding a toolchain variable without this change causes basically
everything to break I don't think there should be any side effects.

>
> Best regards,
> Romain
>
> >
> > Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> > ---
> >  support/misc/toolchainfile.cmake.in | 108 +++++++++++++++++++++-------
> >  1 file changed, 81 insertions(+), 27 deletions(-)
> >
> > diff --git a/support/misc/toolchainfile.cmake.in b/support/misc/toolchainfile.cmake.in
> > index be575566a4..30c0e63bf2 100644
> > --- a/support/misc/toolchainfile.cmake.in
> > +++ b/support/misc/toolchainfile.cmake.in
> > @@ -14,9 +14,15 @@ string(REPLACE "/share/buildroot" "" RELOCATED_HOST_DIR ${CMAKE_CURRENT_LIST_DIR
> >  # so that it can find our custom platform description.
> >  list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
> >
> > -set(CMAKE_SYSTEM_NAME Buildroot)
> > -set(CMAKE_SYSTEM_VERSION 1)
> > -set(CMAKE_SYSTEM_PROCESSOR @@CMAKE_SYSTEM_PROCESSOR@@)
> > +if(NOT DEFINED CMAKE_SYSTEM_NAME)
> > +  set(CMAKE_SYSTEM_NAME Buildroot)
> > +endif()
> > +if(NOT DEFINED CMAKE_SYSTEM_VERSION)
> > +  set(CMAKE_SYSTEM_VERSION 1)
> > +endif()
> > +if(NOT DEFINED CMAKE_SYSTEM_PROCESSOR)
> > +  set(CMAKE_SYSTEM_PROCESSOR @@CMAKE_SYSTEM_PROCESSOR@@)
> > +endif()
> >
> >  # Set the {C,CXX}FLAGS appended by CMake depending on the build type
> >  # defined by Buildroot. CMake defaults these variables with -g and/or
> > @@ -28,11 +34,17 @@ set(CMAKE_SYSTEM_PROCESSOR @@CMAKE_SYSTEM_PROCESSOR@@)
> >  # Note:
> >  #   if the project forces some of these flag variables, Buildroot is
> >  #   screwed up and there is nothing Buildroot can do about that :(
> > -set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "Debug CFLAGS")
> > -set(CMAKE_C_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release CFLAGS")
> > +if(NOT DEFINED CMAKE_C_FLAGS_DEBUG)
> > +  set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "Debug CFLAGS")
> > +endif()
> > +if(NOT DEFINED CMAKE_C_FLAGS_RELEASE)
> > +  set(CMAKE_C_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release CFLAGS")
> > +endif()
> >
> >  # Build type from the Buildroot configuration
> > -set(CMAKE_BUILD_TYPE @@CMAKE_BUILD_TYPE@@ CACHE STRING "Buildroot build configuration")
> > +if(NOT DEFINED CMAKE_BUILD_TYPE)
> > +  set(CMAKE_BUILD_TYPE @@CMAKE_BUILD_TYPE@@ CACHE STRING "Buildroot build configuration")
> > +endif()
> >
> >  # Buildroot defaults flags.
> >  # If you are using this toolchainfile.cmake file outside of Buildroot and
> > @@ -41,33 +53,75 @@ set(CMAKE_BUILD_TYPE @@CMAKE_BUILD_TYPE@@ CACHE STRING "Buildroot build configur
> >  #     cmake -DCMAKE_C_FLAGS="@@TARGET_CFLAGS@@ -Dsome_custom_flag" ...
> >  # * and make sure the project's CMake code extends them like this if needed:
> >  #     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Dsome_definitions")
> > -set(CMAKE_C_FLAGS "@@TARGET_CFLAGS@@" CACHE STRING "Buildroot CFLAGS")
> > -set(CMAKE_EXE_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for executables")
> > -set(CMAKE_SHARED_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for shared libraries")
> > -set(CMAKE_MODULE_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for module libraries")
> > +if(NOT DEFINED CMAKE_C_FLAGS)
> > +  set(CMAKE_C_FLAGS "@@TARGET_CFLAGS@@" CACHE STRING "Buildroot CFLAGS")
> > +endif()
> > +if(NOT DEFINED CMAKE_EXE_LINKER_FLAGS)
> > +  set(CMAKE_EXE_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for executables")
> > +endif()
> > +if(NOT DEFINED CMAKE_SHARED_LINKER_FLAGS)
> > +  set(CMAKE_SHARED_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for shared libraries")
> > +endif()
> > +if(NOT DEFINED CMAKE_MODULE_LINKER_FLAGS)
> > +  set(CMAKE_MODULE_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for module libraries")
> > +endif()
> >
> > -set(CMAKE_INSTALL_SO_NO_EXE 0)
> > +if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)
> > +  set(CMAKE_INSTALL_SO_NO_EXE 0)
> > +endif()
> >
> > -set(CMAKE_PROGRAM_PATH "${RELOCATED_HOST_DIR}/bin")
> > -set(CMAKE_SYSROOT "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
> > -set(CMAKE_FIND_ROOT_PATH "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
> > -set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
> > -set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
> > -set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
> > -set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
> > +if(NOT DEFINED CMAKE_PROGRAM_PATH)
> > +  set(CMAKE_PROGRAM_PATH "${RELOCATED_HOST_DIR}/bin")
> > +endif()
> > +if(NOT DEFINED CMAKE_SYSROOT)
> > +  set(CMAKE_SYSROOT "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
> > +endif()
> > +if(NOT DEFINED CMAKE_FIND_ROOT_PATH)
> > +  set(CMAKE_FIND_ROOT_PATH "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
> > +endif()
> > +if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PROGRAM)
> > +  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
> > +endif()
> > +if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PACKAGE)
> > +  set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
> > +endif()
> > +if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
> > +  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
> > +endif()
> > +if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)
> > +  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
> > +endif()
> >  set(ENV{PKG_CONFIG_SYSROOT_DIR} "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
> >
> >  # This toolchain file can be used both inside and outside Buildroot.
> > -set(CMAKE_C_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_CC@@")
> > +if(NOT DEFINED CMAKE_C_COMPILER)
> > +  set(CMAKE_C_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_CC@@")
> > +endif()
> >  if(@@TOOLCHAIN_HAS_CXX@@)
> > -  set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "Debug CXXFLAGS")
> > -  set(CMAKE_CXX_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release CXXFLAGS")
> > -  set(CMAKE_CXX_FLAGS "@@TARGET_CXXFLAGS@@" CACHE STRING "Buildroot CXXFLAGS")
> > -  set(CMAKE_CXX_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_CXX@@")
> > +  if(NOT DEFINED CMAKE_CXX_FLAGS_DEBUG)
> > +    set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "Debug CXXFLAGS")
> > +  endif()
> > +  if(NOT DEFINED CMAKE_CXX_FLAGS_RELEASE)
> > +    set(CMAKE_CXX_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release CXXFLAGS")
> > +  endif()
> > +  if(NOT DEFINED CMAKE_CXX_FLAGS)
> > +    set(CMAKE_CXX_FLAGS "@@TARGET_CXXFLAGS@@" CACHE STRING "Buildroot CXXFLAGS")
> > +  endif()
> > +  if(NOT DEFINED CMAKE_CXX_COMPILER)
> > +    set(CMAKE_CXX_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_CXX@@")
> > +  endif()
> >  endif()
> >  if(@@TOOLCHAIN_HAS_FORTRAN@@)
> > -  set(CMAKE_Fortran_FLAGS_DEBUG "" CACHE STRING "Debug Fortran FLAGS")
> > -  set(CMAKE_Fortran_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release Fortran FLAGS")
> > -  set(CMAKE_Fortran_FLAGS "@@TARGET_FCFLAGS@@" CACHE STRING "Buildroot FCFLAGS")
> > -  set(CMAKE_Fortran_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_FC@@")
> > +  if(NOT DEFINED CMAKE_Fortran_FLAGS_DEBUG)
> > +    set(CMAKE_Fortran_FLAGS_DEBUG "" CACHE STRING "Debug Fortran FLAGS")
> > +  endif()
> > +  if(NOT DEFINED CMAKE_Fortran_FLAGS_RELEASE)
> > +    set(CMAKE_Fortran_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release Fortran FLAGS")
> > +  endif()
> > +  if(NOT DEFINED CMAKE_Fortran_FLAGS)
> > +    set(CMAKE_Fortran_FLAGS "@@TARGET_FCFLAGS@@" CACHE STRING "Buildroot FCFLAGS")
> > +  endif()
> > +  if(NOT DEFINED CMAKE_Fortran_COMPILER)
> > +    set(CMAKE_Fortran_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_FC@@")
> > +  endif()
> >  endif()
>
diff mbox series

Patch

diff --git a/support/misc/toolchainfile.cmake.in b/support/misc/toolchainfile.cmake.in
index be575566a4..30c0e63bf2 100644
--- a/support/misc/toolchainfile.cmake.in
+++ b/support/misc/toolchainfile.cmake.in
@@ -14,9 +14,15 @@  string(REPLACE "/share/buildroot" "" RELOCATED_HOST_DIR ${CMAKE_CURRENT_LIST_DIR
 # so that it can find our custom platform description.
 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
 
-set(CMAKE_SYSTEM_NAME Buildroot)
-set(CMAKE_SYSTEM_VERSION 1)
-set(CMAKE_SYSTEM_PROCESSOR @@CMAKE_SYSTEM_PROCESSOR@@)
+if(NOT DEFINED CMAKE_SYSTEM_NAME)
+  set(CMAKE_SYSTEM_NAME Buildroot)
+endif()
+if(NOT DEFINED CMAKE_SYSTEM_VERSION)
+  set(CMAKE_SYSTEM_VERSION 1)
+endif()
+if(NOT DEFINED CMAKE_SYSTEM_PROCESSOR)
+  set(CMAKE_SYSTEM_PROCESSOR @@CMAKE_SYSTEM_PROCESSOR@@)
+endif()
 
 # Set the {C,CXX}FLAGS appended by CMake depending on the build type
 # defined by Buildroot. CMake defaults these variables with -g and/or
@@ -28,11 +34,17 @@  set(CMAKE_SYSTEM_PROCESSOR @@CMAKE_SYSTEM_PROCESSOR@@)
 # Note:
 #   if the project forces some of these flag variables, Buildroot is
 #   screwed up and there is nothing Buildroot can do about that :(
-set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "Debug CFLAGS")
-set(CMAKE_C_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release CFLAGS")
+if(NOT DEFINED CMAKE_C_FLAGS_DEBUG)
+  set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "Debug CFLAGS")
+endif()
+if(NOT DEFINED CMAKE_C_FLAGS_RELEASE)
+  set(CMAKE_C_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release CFLAGS")
+endif()
 
 # Build type from the Buildroot configuration
-set(CMAKE_BUILD_TYPE @@CMAKE_BUILD_TYPE@@ CACHE STRING "Buildroot build configuration")
+if(NOT DEFINED CMAKE_BUILD_TYPE)
+  set(CMAKE_BUILD_TYPE @@CMAKE_BUILD_TYPE@@ CACHE STRING "Buildroot build configuration")
+endif()
 
 # Buildroot defaults flags.
 # If you are using this toolchainfile.cmake file outside of Buildroot and
@@ -41,33 +53,75 @@  set(CMAKE_BUILD_TYPE @@CMAKE_BUILD_TYPE@@ CACHE STRING "Buildroot build configur
 #     cmake -DCMAKE_C_FLAGS="@@TARGET_CFLAGS@@ -Dsome_custom_flag" ...
 # * and make sure the project's CMake code extends them like this if needed:
 #     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Dsome_definitions")
-set(CMAKE_C_FLAGS "@@TARGET_CFLAGS@@" CACHE STRING "Buildroot CFLAGS")
-set(CMAKE_EXE_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for executables")
-set(CMAKE_SHARED_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for shared libraries")
-set(CMAKE_MODULE_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for module libraries")
+if(NOT DEFINED CMAKE_C_FLAGS)
+  set(CMAKE_C_FLAGS "@@TARGET_CFLAGS@@" CACHE STRING "Buildroot CFLAGS")
+endif()
+if(NOT DEFINED CMAKE_EXE_LINKER_FLAGS)
+  set(CMAKE_EXE_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for executables")
+endif()
+if(NOT DEFINED CMAKE_SHARED_LINKER_FLAGS)
+  set(CMAKE_SHARED_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for shared libraries")
+endif()
+if(NOT DEFINED CMAKE_MODULE_LINKER_FLAGS)
+  set(CMAKE_MODULE_LINKER_FLAGS "@@TARGET_LDFLAGS@@" CACHE STRING "Buildroot LDFLAGS for module libraries")
+endif()
 
-set(CMAKE_INSTALL_SO_NO_EXE 0)
+if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)
+  set(CMAKE_INSTALL_SO_NO_EXE 0)
+endif()
 
-set(CMAKE_PROGRAM_PATH "${RELOCATED_HOST_DIR}/bin")
-set(CMAKE_SYSROOT "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
-set(CMAKE_FIND_ROOT_PATH "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
-set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
-set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
-set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
-set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+if(NOT DEFINED CMAKE_PROGRAM_PATH)
+  set(CMAKE_PROGRAM_PATH "${RELOCATED_HOST_DIR}/bin")
+endif()
+if(NOT DEFINED CMAKE_SYSROOT)
+  set(CMAKE_SYSROOT "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
+endif()
+if(NOT DEFINED CMAKE_FIND_ROOT_PATH)
+  set(CMAKE_FIND_ROOT_PATH "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
+endif()
+if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PROGRAM)
+  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+endif()
+if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PACKAGE)
+  set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+endif()
+if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
+  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+endif()
+if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)
+  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+endif()
 set(ENV{PKG_CONFIG_SYSROOT_DIR} "${RELOCATED_HOST_DIR}/@@STAGING_SUBDIR@@")
 
 # This toolchain file can be used both inside and outside Buildroot.
-set(CMAKE_C_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_CC@@")
+if(NOT DEFINED CMAKE_C_COMPILER)
+  set(CMAKE_C_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_CC@@")
+endif()
 if(@@TOOLCHAIN_HAS_CXX@@)
-  set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "Debug CXXFLAGS")
-  set(CMAKE_CXX_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release CXXFLAGS")
-  set(CMAKE_CXX_FLAGS "@@TARGET_CXXFLAGS@@" CACHE STRING "Buildroot CXXFLAGS")
-  set(CMAKE_CXX_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_CXX@@")
+  if(NOT DEFINED CMAKE_CXX_FLAGS_DEBUG)
+    set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "Debug CXXFLAGS")
+  endif()
+  if(NOT DEFINED CMAKE_CXX_FLAGS_RELEASE)
+    set(CMAKE_CXX_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release CXXFLAGS")
+  endif()
+  if(NOT DEFINED CMAKE_CXX_FLAGS)
+    set(CMAKE_CXX_FLAGS "@@TARGET_CXXFLAGS@@" CACHE STRING "Buildroot CXXFLAGS")
+  endif()
+  if(NOT DEFINED CMAKE_CXX_COMPILER)
+    set(CMAKE_CXX_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_CXX@@")
+  endif()
 endif()
 if(@@TOOLCHAIN_HAS_FORTRAN@@)
-  set(CMAKE_Fortran_FLAGS_DEBUG "" CACHE STRING "Debug Fortran FLAGS")
-  set(CMAKE_Fortran_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release Fortran FLAGS")
-  set(CMAKE_Fortran_FLAGS "@@TARGET_FCFLAGS@@" CACHE STRING "Buildroot FCFLAGS")
-  set(CMAKE_Fortran_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_FC@@")
+  if(NOT DEFINED CMAKE_Fortran_FLAGS_DEBUG)
+    set(CMAKE_Fortran_FLAGS_DEBUG "" CACHE STRING "Debug Fortran FLAGS")
+  endif()
+  if(NOT DEFINED CMAKE_Fortran_FLAGS_RELEASE)
+    set(CMAKE_Fortran_FLAGS_RELEASE " -DNDEBUG" CACHE STRING "Release Fortran FLAGS")
+  endif()
+  if(NOT DEFINED CMAKE_Fortran_FLAGS)
+    set(CMAKE_Fortran_FLAGS "@@TARGET_FCFLAGS@@" CACHE STRING "Buildroot FCFLAGS")
+  endif()
+  if(NOT DEFINED CMAKE_Fortran_COMPILER)
+    set(CMAKE_Fortran_COMPILER "${RELOCATED_HOST_DIR}/@@TARGET_FC@@")
+  endif()
 endif()