2008-03-25 18:27:18 +03:00
|
|
|
cmake_minimum_required (VERSION 2.6)
|
2012-08-13 21:47:32 +04:00
|
|
|
project(StringFileTest)
|
|
|
|
include_directories(${StringFileTest_BINARY_DIR})
|
2003-07-10 22:29:30 +04:00
|
|
|
|
2003-07-21 21:48:18 +04:00
|
|
|
# Read file test
|
2012-08-13 21:47:32 +04:00
|
|
|
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in" infile)
|
2003-07-21 21:48:18 +04:00
|
|
|
|
2009-09-06 18:26:24 +04:00
|
|
|
# Test reading a binary file into hex representation
|
2012-08-13 21:47:32 +04:00
|
|
|
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/test.bin" hexContents HEX)
|
2009-09-06 18:26:24 +04:00
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
if("${hexContents}" STREQUAL "0001027700")
|
|
|
|
message("file(READ HEX) correctly read [${hexContents}]")
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
message(SEND_ERROR "file(READ HEX) incorrectly read [${hexContents}], but expected was [0001027700]")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2009-09-06 18:26:24 +04:00
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
# file(STRINGS) test
|
|
|
|
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in" infile_strings
|
2007-04-26 01:22:07 +04:00
|
|
|
LIMIT_COUNT 1 LIMIT_INPUT 1024 LIMIT_OUTPUT 1024
|
|
|
|
LENGTH_MINIMUM 10 LENGTH_MAXIMUM 23 REGEX include NEWLINE_CONSUME)
|
2012-08-13 21:47:32 +04:00
|
|
|
set(infile_strings_goal "#include \"includefile\"\n")
|
|
|
|
if("${infile_strings}" STREQUAL "${infile_strings_goal}")
|
|
|
|
message("file(STRINGS) correctly read [${infile_strings}]")
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
message(SEND_ERROR
|
|
|
|
"file(STRINGS) incorrectly read [${infile_strings}]")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2007-04-26 01:22:07 +04:00
|
|
|
|
2008-01-03 02:00:19 +03:00
|
|
|
# test reading a file and getting its binary data as hex string
|
2012-08-13 21:47:32 +04:00
|
|
|
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/main.srec" infilehex LIMIT 4 HEX)
|
|
|
|
if(NOT "${infilehex}" STREQUAL "53313036")
|
|
|
|
message(SEND_ERROR
|
|
|
|
"file(READ ... HEX) error, read: \"${infilehex}\", expected \"53313036\"")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2008-01-03 02:00:19 +03:00
|
|
|
|
2007-05-25 23:22:22 +04:00
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
# test that file(STRINGS) also work with Intel hex and Motorola S-record files
|
2007-05-25 23:22:22 +04:00
|
|
|
# this file has been created with "sdcc main.c"
|
2012-08-13 21:47:32 +04:00
|
|
|
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/main.ihx" infile_strings REGEX INFO)
|
|
|
|
set(infile_strings_goal "INFO:compiler\\[SDCC-HEX\\]")
|
|
|
|
if("${infile_strings}" MATCHES "${infile_strings_goal}")
|
|
|
|
message("file(STRINGS) correctly read from hex file [${infile_strings}]")
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
message(SEND_ERROR
|
|
|
|
"file(STRINGS) incorrectly read from hex file [${infile_strings}]")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2007-05-25 23:22:22 +04:00
|
|
|
|
|
|
|
# this file has been created with "sdcc main.c --out-fmt-s19"
|
2012-08-13 21:47:32 +04:00
|
|
|
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/main.srec" infile_strings REGEX INFO)
|
|
|
|
set(infile_strings_goal "INFO:compiler\\[SDCC-SREC\\]")
|
|
|
|
if("${infile_strings}" MATCHES "${infile_strings_goal}")
|
|
|
|
message("file(STRINGS) correctly read from srec file [${infile_strings}]")
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
message(SEND_ERROR
|
|
|
|
"file(STRINGS) incorrectly read from srec file [${infile_strings}]")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2007-05-25 23:22:22 +04:00
|
|
|
|
2003-07-21 21:48:18 +04:00
|
|
|
# String test
|
2012-08-13 21:47:32 +04:00
|
|
|
string(REGEX MATCH "[cC][mM][aA][kK][eE]" rmvar "CMake is great")
|
|
|
|
string(REGEX MATCHALL "[cC][mM][aA][kK][eE]" rmallvar "CMake is better than cmake or CMake")
|
|
|
|
string(REGEX REPLACE "[Aa][uU][tT][oO]([cC][oO][nN][fF]|[mM][aA][kK][eE])"
|
2003-07-10 22:29:30 +04:00
|
|
|
"CMake" rrepvar "People should use Autoconf and Automake")
|
2012-08-13 21:47:32 +04:00
|
|
|
string(COMPARE EQUAL "CMake" "Autoconf" nceqvar)
|
|
|
|
string(COMPARE EQUAL "CMake" "CMake" ceqvar)
|
|
|
|
string(COMPARE NOTEQUAL "CMake" "Autoconf" cneqvar)
|
|
|
|
string(COMPARE NOTEQUAL "CMake" "CMake" ncneqvar)
|
|
|
|
string(COMPARE LESS "before" "after" nclvar)
|
|
|
|
string(COMPARE LESS "max" "min" clvar)
|
|
|
|
string(COMPARE GREATER "before" "after" cgvar)
|
|
|
|
string(COMPARE GREATER "max" "min" ncgvar)
|
|
|
|
string(ASCII 67 109 97 107 101 savar)
|
|
|
|
string(TOUPPER "CMake" tuvar)
|
|
|
|
string(TOLOWER "CMake" tlvar)
|
|
|
|
string(REPLACE "Autoconf" "CMake" repvar "People should use Autoconf")
|
|
|
|
|
|
|
|
if("abc" STREQUAL "xyz")
|
|
|
|
message(SEND_ERROR "Problem with the if(STREQUAL), \"abc\" and \"xyz\" considered equal")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
|
|
|
|
if("CMake is cool" MATCHES "(CMake) (is).+")
|
|
|
|
if(NOT "${CMAKE_MATCH_0}" STREQUAL "CMake is cool")
|
|
|
|
message(SEND_ERROR "CMAKE_MATCH_0 wrong: \"${CMAKE_MATCH_0}\", expected \"CMake is cool\"")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
if(NOT "${CMAKE_MATCH_1}" STREQUAL "CMake")
|
|
|
|
message(SEND_ERROR "CMAKE_MATCH_1 wrong: \"${CMAKE_MATCH_1}\", expected \"CMake\"")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
if(NOT "${CMAKE_MATCH_2}" STREQUAL "is")
|
|
|
|
message(SEND_ERROR "CMAKE_MATCH_2 wrong: \"${CMAKE_MATCH_2}\", expected \"is\"")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
message(SEND_ERROR "Problem with the if(MATCHES), no match found")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
|
|
|
|
string(REGEX MATCH "(People).+CMake" matchResultVar "People should use CMake")
|
|
|
|
if(NOT "${matchResultVar}" STREQUAL "People should use CMake")
|
|
|
|
message(SEND_ERROR "string(REGEX MATCH) problem: \"${matchResultVar}\", expected \"People should use CMake\"")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
if(NOT "${CMAKE_MATCH_0}" STREQUAL "People should use CMake")
|
|
|
|
message(SEND_ERROR "CMAKE_MATCH_0 wrong: \"${CMAKE_MATCH_0}\", expected \"People should use CMake\"")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
if(NOT "${CMAKE_MATCH_1}" STREQUAL "People")
|
|
|
|
message(SEND_ERROR "CMAKE_MATCH_1 wrong: \"${CMAKE_MATCH_1}\", expected \"People\"")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
if(NOT "${CMAKE_MATCH_2}" STREQUAL "")
|
|
|
|
message(SEND_ERROR "CMAKE_MATCH_2 wrong: \"${CMAKE_MATCH_2}\", expected empty string")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
string(STRIP "
|
2007-04-27 05:50:52 +04:00
|
|
|
ST1
|
|
|
|
" ST1)
|
2012-08-13 21:47:32 +04:00
|
|
|
string(STRIP "ST2 " ST2)
|
|
|
|
string(STRIP " ST3" ST3)
|
2007-04-27 05:50:52 +04:00
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
foreach(var ST1 ST2 ST3)
|
|
|
|
if("${var}" STREQUAL "${${var}}")
|
|
|
|
message("[${var}] == [${${var}}]")
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
message(SEND_ERROR "Problem with the STRIP command for ${var}: [${${var}}]")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
|
|
|
endforeach()
|
2007-04-27 05:50:52 +04:00
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
string(SUBSTRING "People should use Autoconf" 7 10 substringres)
|
|
|
|
set(substringres "Everybody ${substringres} CMake")
|
2005-10-17 17:56:42 +04:00
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
string(LENGTH ${substringres} lengthres)
|
2005-10-17 17:56:42 +04:00
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
file(RELATIVE_PATH relpath "/usr/local/bin" "/usr/X11R6/bin/xnest")
|
2003-07-10 22:29:30 +04:00
|
|
|
|
2004-09-20 16:51:12 +04:00
|
|
|
# Make-style unquoted argument test
|
2012-08-13 21:47:32 +04:00
|
|
|
set(var $(VAR1)$(VAR2)/$(VAR3))
|
|
|
|
message("Output: [${var}]")
|
|
|
|
string(COMPARE EQUAL "${var}" "$(VAR1)$(VAR2)/$(VAR3)" result)
|
|
|
|
if(NOT result)
|
|
|
|
message(SEND_ERROR "Unquoted $(VAR) syntax is broken.")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2004-09-20 16:51:12 +04:00
|
|
|
|
2003-07-21 21:48:18 +04:00
|
|
|
# Make directories test
|
2012-08-13 21:47:32 +04:00
|
|
|
file(MAKE_DIRECTORY
|
2012-08-13 21:42:58 +04:00
|
|
|
"${CMAKE_CURRENT_BINARY_DIR}/Includes"
|
2003-07-21 21:48:18 +04:00
|
|
|
"${CMAKE_CURRENT_BINARY_DIR}/Directory1"
|
|
|
|
"${CMAKE_CURRENT_BINARY_DIR}/Directory2"
|
|
|
|
)
|
2003-07-10 22:29:30 +04:00
|
|
|
|
2003-07-21 21:48:18 +04:00
|
|
|
# Write results to the file (test write file)
|
2012-08-13 21:47:32 +04:00
|
|
|
set(file "${CMAKE_CURRENT_BINARY_DIR}/Includes/Values.h")
|
|
|
|
file(WRITE "${file}" "/* this file is generated */\n")
|
|
|
|
foreach(var
|
2003-07-10 22:29:30 +04:00
|
|
|
rmvar
|
|
|
|
rmallvar
|
|
|
|
rrepvar
|
2005-10-17 17:10:20 +04:00
|
|
|
repvar
|
|
|
|
relpath
|
2005-10-17 17:56:42 +04:00
|
|
|
substringres
|
|
|
|
lengthres
|
2003-07-10 22:29:30 +04:00
|
|
|
nceqvar
|
|
|
|
ceqvar
|
|
|
|
cneqvar
|
|
|
|
ncneqvar
|
|
|
|
nclvar
|
|
|
|
clvar
|
|
|
|
cgvar
|
|
|
|
ncgvar
|
|
|
|
savar
|
|
|
|
tuvar
|
|
|
|
tlvar)
|
2012-08-13 21:47:32 +04:00
|
|
|
file(APPEND "${file}" "#define ${var} \"${${var}}\"\n")
|
2012-08-13 21:50:14 +04:00
|
|
|
endforeach()
|
2003-07-10 22:29:30 +04:00
|
|
|
|
2006-08-26 00:31:07 +04:00
|
|
|
# Verify that the file was created recently.
|
2012-08-13 21:47:32 +04:00
|
|
|
if(NOT "${file}" IS_NEWER_THAN "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in")
|
|
|
|
message(FATAL_ERROR "if(FILE_IS_NEWER) does not seem to work.")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2006-08-26 00:31:07 +04:00
|
|
|
|
2004-03-04 18:05:14 +03:00
|
|
|
# Test configuration of the string
|
2012-08-13 21:47:32 +04:00
|
|
|
set(TEST_DEFINED 123)
|
|
|
|
set(TEST_NOT_DEFINED)
|
|
|
|
string(CONFIGURE "${infile}" infile+-/out @ONLY)
|
|
|
|
set(infile "${infile+-/out}")
|
2004-03-04 18:05:14 +03:00
|
|
|
|
2003-07-21 21:48:18 +04:00
|
|
|
# Write include file to a file
|
2012-08-13 21:47:32 +04:00
|
|
|
string(REGEX REPLACE "includefile" "${file}" outfile "${infile}")
|
|
|
|
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/OutputFile.h-tmp" "${outfile}")
|
|
|
|
file(RENAME "${CMAKE_CURRENT_BINARY_DIR}/OutputFile.h-tmp"
|
2009-04-15 17:58:13 +04:00
|
|
|
"${CMAKE_CURRENT_BINARY_DIR}/OutputFile.h")
|
2003-07-10 22:29:30 +04:00
|
|
|
|
2009-04-29 21:13:29 +04:00
|
|
|
# Test file copy with relative paths
|
2012-08-13 21:47:32 +04:00
|
|
|
file(COPY .
|
2009-04-29 21:13:29 +04:00
|
|
|
DESTINATION src
|
2009-05-13 18:30:22 +04:00
|
|
|
FILE_PERMISSIONS OWNER_READ # test no OWNER_WRITE
|
|
|
|
DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
|
2009-04-29 21:13:29 +04:00
|
|
|
FILES_MATCHING PATTERN *.cxx # Only copy the main source file
|
|
|
|
REGEX /src$ EXCLUDE # Block recursion for in-source build
|
|
|
|
)
|
|
|
|
|
2003-07-21 21:48:18 +04:00
|
|
|
# Test file glob
|
2012-08-13 21:47:32 +04:00
|
|
|
file(GLOB_RECURSE src_files "${CMAKE_CURRENT_SOURCE_DIR}/*")
|
|
|
|
message("Files in ${CMAKE_CURRENT_SOURCE_DIR} are ${src_files}")
|
|
|
|
set(expr "${CMAKE_CURRENT_BINARY_DIR}/src/[sS][!a-su-zA-Z0-9][^a-qs-zA-Z0-9]ing?ile*.cxx")
|
|
|
|
message("Glob expression is [${expr}].")
|
|
|
|
file(GLOB src_files "${expr}")
|
|
|
|
message("Globbed files [${src_files}].")
|
|
|
|
add_executable(StringFileTest ${src_files})
|
|
|
|
|
|
|
|
set(expr "${CMAKE_CURRENT_SOURCE_DIR}/../*.cxx")
|
|
|
|
file(GLOB_RECURSE rel_src_files RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/.." "${expr}")
|
|
|
|
message("Globbed files [${rel_src_files}].")
|
2006-03-22 00:03:19 +03:00
|
|
|
|
2004-04-29 23:12:40 +04:00
|
|
|
# Test FOREACH range
|
2012-08-13 21:47:32 +04:00
|
|
|
message("Cheack if FOREACH with RANGE works")
|
|
|
|
macro(TEST_RANGE ARGS CHECK)
|
|
|
|
set(r)
|
|
|
|
foreach(a RANGE ${ARGS})
|
|
|
|
set(r ${r} ${a})
|
2012-08-13 21:50:14 +04:00
|
|
|
endforeach()
|
2012-08-13 21:47:32 +04:00
|
|
|
message("FOREACH with RANGE ${ARGS} produces ${r}")
|
|
|
|
if("x${r}x" MATCHES "^x${CHECK}x$")
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
message(SEND_ERROR "The range resulted in: ${r} should be ${CHECK}")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
|
|
|
endmacro()
|
2004-04-29 23:12:40 +04:00
|
|
|
TEST_RANGE("5" "0;1;2;3;4;5")
|
|
|
|
TEST_RANGE("3;5" "3;4;5")
|
|
|
|
TEST_RANGE("5;3" "5;4;3")
|
|
|
|
TEST_RANGE("3;10;2" "3;5;7;9")
|
|
|
|
TEST_RANGE("10;0;-3" "10;7;4;1")
|
2009-03-17 22:10:15 +03:00
|
|
|
|
|
|
|
# Test FOREACH IN signature
|
|
|
|
set(list1 "" a "")
|
|
|
|
set(list2 a "" b)
|
|
|
|
set(var_)
|
|
|
|
set(var_a)
|
|
|
|
set(var_b)
|
|
|
|
foreach(item IN LISTS list1 list2 ITEMS "" a "")
|
|
|
|
set(var_${item} "${var_${item}}x")
|
2012-08-13 21:50:14 +04:00
|
|
|
endforeach()
|
2009-03-17 22:10:15 +03:00
|
|
|
if(NOT "${var_}" STREQUAL "xxxxx")
|
|
|
|
message(FATAL_ERROR "count incorrect for \"\": [${var_}]")
|
|
|
|
endif()
|
|
|
|
if(NOT "${var_a}" STREQUAL "xxx")
|
|
|
|
message(FATAL_ERROR "count incorrect for \"a\": [${var_a}]")
|
|
|
|
endif()
|
|
|
|
if(NOT "${var_b}" STREQUAL "x")
|
|
|
|
message(FATAL_ERROR "count incorrect \"b\": [${var_b}]")
|
|
|
|
endif()
|
2010-07-04 20:56:05 +04:00
|
|
|
|
|
|
|
# Test SUBSTRING command
|
2012-08-13 21:47:32 +04:00
|
|
|
set(ST_INPUTSTRING "0123456789")
|
|
|
|
string(SUBSTRING ${ST_INPUTSTRING} 3 0 ST_EMPTY)
|
|
|
|
string(SUBSTRING ${ST_INPUTSTRING} 1 1 ST_ONE)
|
|
|
|
string(SUBSTRING ${ST_INPUTSTRING} 0 10 ST_ALL)
|
|
|
|
string(SUBSTRING ${ST_INPUTSTRING} 0 -1 ST_ALL_MINUS)
|
|
|
|
string(SUBSTRING ${ST_INPUTSTRING} 9 -1 ST_NINE)
|
|
|
|
|
|
|
|
if(ST_EMPTY)
|
|
|
|
message(SEND_ERROR "SUBSTRING with length 0 does not return an empty string")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
if(NOT ST_ONE STREQUAL "1")
|
|
|
|
message(SEND_ERROR "SUBSTING command does not cut the correct selected character, was \"" ${ST_ONE} "\", should be \"1\"")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
if(NOT ST_INPUTSTRING STREQUAL ST_ALL)
|
|
|
|
message(SEND_ERROR "SUBSTRING does not return the whole string when selected with length")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
if(NOT ST_INPUTSTRING STREQUAL ST_ALL_MINUS)
|
|
|
|
message(SEND_ERROR "SUBSTRING does not return the whole string when selected with -1")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
if(NOT ST_NINE STREQUAL "9")
|
|
|
|
message(SEND_ERROR "SUBSTRING does not return the tail when selected with -1")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2013-03-21 22:34:32 +04:00
|
|
|
|
|
|
|
string(MAKE_C_IDENTIFIER "1one-two$" MCI_1)
|
|
|
|
|
|
|
|
if(NOT MCI_1 STREQUAL _1one_two_)
|
|
|
|
message(SEND_ERROR "MAKE_C_IDENTIFIER did not create expected result.")
|
|
|
|
endif()
|
2014-03-18 19:21:08 +04:00
|
|
|
|
|
|
|
string(GENEX_STRIP "one;$<1:two;three>;four;$<TARGET_OBJECTS:some_target>" strip_result)
|
|
|
|
|
|
|
|
if (NOT strip_result STREQUAL "one;four")
|
|
|
|
message(SEND_ERROR "GENEX_STRIP did not create expected result: ${strip_result}")
|
|
|
|
endif()
|