Question about IF and STRINGS

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about IF and STRINGS

Steven Truppe

Hi everyone,

i try to create a build system where you can decide which libraries you want to use with variables like "WITH_LIB_GLFW" for example. every lib variable has other variables like WITH_LIB_GLFW_INC_PATH,

WITH_LIB_GLFW_LIB_PATH, etc. so i can decide with cmake what libraries i'm going to use for the final executable.

Now my problem is that i'm relative new to cmake and i'm learning c++11 in the meantime and are coding on a project so i don't have to much time for cmake (but it's a realy important part).

Here is the code: 

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
include(CMakePrintHelpers)

set(WITH_LIB_GLAD "Support the GLAD library" 1)
set(WITH_LIB_GLFW "Support for the GLFW library" 1)

 macro(bsAddLibrary lib)
	# get all WITH_LIB varables

	get_cmake_property(_variables VARIABLES)
	foreach(_var ${_variables})
		#	cmake_print_variables(${_var})
		if(${_var} MATCHES "^WITH_LIB_[AZaz]+$" _lib)
			cmake_print_variables(${_lib})
		endif()
	endforeach()
endmacro()


bsAddLibrary(WITH_LIB_GLAD) # Adds WITH_LIB_GLAD_INC_PATH and the other variables to a list for the main application that holds all inc path, compiler flags etc.

 ERROR:
======

-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:15 (if):
  if given arguments:

    "CMAKE_AR" "MATCHES" "^WITH_LIB_[AZaz]+\$" "_lib"

  Unknown arguments specified
Call Stack (most recent call first):
  CMakeLists.txt:22 (bsAddLibrary)

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Question about IF and STRINGS

Steven Truppe

The problem is the line:

if(${_var} MATCHES "^WITH_LIB_([AZaz]+)$")
cmake_print_variables(CMAKE_MATCH_0)

doesn't print me any output ...


On 31.05.19 18:53, Steven Truppe wrote:

Hi everyone,

i try to create a build system where you can decide which libraries you want to use with variables like "WITH_LIB_GLFW" for example. every lib variable has other variables like WITH_LIB_GLFW_INC_PATH,

WITH_LIB_GLFW_LIB_PATH, etc. so i can decide with cmake what libraries i'm going to use for the final executable.

Now my problem is that i'm relative new to cmake and i'm learning c++11 in the meantime and are coding on a project so i don't have to much time for cmake (but it's a realy important part).

Here is the code: 

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
include(CMakePrintHelpers)

set(WITH_LIB_GLAD "Support the GLAD library" 1)
set(WITH_LIB_GLFW "Support for the GLFW library" 1)

 macro(bsAddLibrary lib)
	# get all WITH_LIB varables

	get_cmake_property(_variables VARIABLES)
	foreach(_var ${_variables})
		#	cmake_print_variables(${_var})
		if(${_var} MATCHES "^WITH_LIB_[AZaz]+$" _lib)
			cmake_print_variables(${_lib})
		endif()
	endforeach()
endmacro()


bsAddLibrary(WITH_LIB_GLAD) # Adds WITH_LIB_GLAD_INC_PATH and the other variables to a list for the main application that holds all inc path, compiler flags etc.

 ERROR:
======

-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:15 (if):
  if given arguments:

    "CMAKE_AR" "MATCHES" "^WITH_LIB_[AZaz]+\$" "_lib"

  Unknown arguments specified
Call Stack (most recent call first):
  CMakeLists.txt:22 (bsAddLibrary)


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Question about IF and STRINGS

CMake mailing list
On Fri, 2019-05-31 at 19:07 +0200, Steven Truppe wrote:
> The problem is the line:
> if(${_var} MATCHES "^WITH_LIB_([AZaz]+)$")
> cmake_print_variables(CMAKE_MATCH_0)
>
> doesn't print me any output ...

There are two problems with the following line:

if(${_var} MATCHES "^WITH_LIB_([AZaz]+)$")

The first argument should be _var instead of ${_var}. See the if()
documentation for more information:

https://cmake.org/cmake/help/v3.14/command/if.html#variable-expansion

The second problem is your regex. I'm guessing you were trying to do:

^WITH_LIB_([A-Za-z]+)$

Notice the hyphens in the character class brackets. This will get you
every character from A to Z and from a to z. Using just [AZaz] will
only match A, Z, a, and z (none of the letters in between.)

So, if you use the following instead:

if(_var MATCHES "^WITH_LIB_([A-Za-z]+)$")
  cmake_print_variables(CMAKE_MATCH_0)

this should do what you want.

Kyle
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Question about IF and STRINGS

Steven Truppe
In reply to this post by Steven Truppe

Found the problem, my regex was wrong [AZaz] should be [A-Za-z] ...

On 31.05.19 19:07, Steven Truppe wrote:

The problem is the line:

if(${_var} MATCHES "^WITH_LIB_([AZaz]+)$")
cmake_print_variables(CMAKE_MATCH_0)

doesn't print me any output ...


On 31.05.19 18:53, Steven Truppe wrote:

Hi everyone,

i try to create a build system where you can decide which libraries you want to use with variables like "WITH_LIB_GLFW" for example. every lib variable has other variables like WITH_LIB_GLFW_INC_PATH,

WITH_LIB_GLFW_LIB_PATH, etc. so i can decide with cmake what libraries i'm going to use for the final executable.

Now my problem is that i'm relative new to cmake and i'm learning c++11 in the meantime and are coding on a project so i don't have to much time for cmake (but it's a realy important part).

Here is the code: 

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
include(CMakePrintHelpers)

set(WITH_LIB_GLAD "Support the GLAD library" 1)
set(WITH_LIB_GLFW "Support for the GLFW library" 1)

 macro(bsAddLibrary lib)
	# get all WITH_LIB varables

	get_cmake_property(_variables VARIABLES)
	foreach(_var ${_variables})
		#	cmake_print_variables(${_var})
		if(${_var} MATCHES "^WITH_LIB_[AZaz]+$" _lib)
			cmake_print_variables(${_lib})
		endif()
	endforeach()
endmacro()


bsAddLibrary(WITH_LIB_GLAD) # Adds WITH_LIB_GLAD_INC_PATH and the other variables to a list for the main application that holds all inc path, compiler flags etc.

 ERROR:
======

-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:15 (if):
  if given arguments:

    "CMAKE_AR" "MATCHES" "^WITH_LIB_[AZaz]+\$" "_lib"

  Unknown arguments specified
Call Stack (most recent call first):
  CMakeLists.txt:22 (bsAddLibrary)



--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake