LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   CMake custom command issue (https://www.linuxquestions.org/questions/programming-9/cmake-custom-command-issue-875092/)

MTK358 04-14-2011 08:28 PM

CMake custom command issue
 
I was having an issue with namespaces and compiling a Lex-generated lexer, so I though that the simplest solution would simply be to wrap namespace constructs around Lex's output before compiling (I know, it's not really an elegant solution, but I couldn't think of anything much better).

Here is a snippet from my CMakeLists.txt:

Code:

find_program (LEX_TOOL NAMES lex flex)
if (NOT LEX_TOOL)
        message (FATAL_ERROR "Unable to find Lex")
endif (NOT LEX_TOOL)

find_program (YACC_TOOL NAMES yacc bison)
if (NOT YACC_TOOL)
        message (FATAL_ERROR "Unable to find YACC")
endif (NOT YACC_TOOL)

find_program (SED_TOOL NAMES sed)
if (NOT SED_TOOL)
        message (FATAL_ERROR "Unable to find sed")
endif (NOT SED_TOOL)

add_custom_command (
        COMMENT "Editing Lex Output"
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lex_lexer.cpp
        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/lex_lexer.cpp.temp
        COMMAND ${SED_TOOL} -e "1 s@.*@#include \"config.hpp\"\\nnamespace LANG_NAMESPACE {\\nnamespace lexyacc {\\n\\n&@" -e "$ s@.*@&\\n}}\\n@" ${CMAKE_CURRENT_BINARY_DIR}/lex_lexer.cpp.temp > ${CMAKE_CURRENT_BINARY_DIR}/lex_lexer.cpp
)

add_custom_command (
        COMMENT "Building Lexer"
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lex_lexer.cpp.temp
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/lex_lexer.l ${CMAKE_CURRENT_BINARY_DIR}/y.tab.hpp
        COMMAND ${LEX_TOOL} -o ${CMAKE_CURRENT_BINARY_DIR}/lex_lexer.cpp.temp ${CMAKE_CURRENT_SOURCE_DIR}/lex_lexer.l
)

add_custom_command (
        COMMENT "Building Parser"
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/y.tab.hpp ${CMAKE_CURRENT_BINARY_DIR}/y.tab.cpp
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/yacc_parser.y
        COMMAND ${YACC_TOOL} -d -o ${CMAKE_CURRENT_BINARY_DIR}/y.tab.cpp ${CMAKE_CURRENT_SOURCE_DIR}/yacc_parser.y
)

The problem is that it spits out this error when I try to make:

Code:

-- Configuring done
-- Generating done
-- Build files have been written to: /home/michael/Projects/lang/build
[  6%] Editing Lex Output
/bin/sh: -c: line 0: unexpected EOF while looking for matching `''
/bin/sh: -c: line 1: syntax error: unexpected end of file
make[2]: *** [lib/lex_lexer.cpp] Error 1
make[1]: *** [lib/CMakeFiles/lang.dir/all] Error 2
make: *** [all] Error 2

What's wrong?

theNbomr 04-15-2011 09:40 AM

Without having analyzed the Makefile in-depth, it looks like there are unbalanced quotes in the line
Code:

        COMMAND ${SED_TOOL} -e "1 s@.*@#include \"config.hpp\"\\nnamespace LANG_NAMESPACE {\\nnamespace lexyacc {\\n\\n&@" -e "$ s@.*@&\\n}}\\n@" ${CMAKE_CURRENT_BINARY_DIR}/lex_lexer.cpp.temp > ${CMAKE_CURRENT_BINARY_DIR}/lex_lexer.cpp
--- rod.

MTK358 04-15-2011 09:50 AM

Quote:

Originally Posted by theNbomr (Post 4325505)
Without having analyzed the Makefile in-depth, it looks like there are unbalanced quotes in the line
Code:

        COMMAND ${SED_TOOL} -e "1 s@.*@#include \"config.hpp\"\\nnamespace LANG_NAMESPACE {\\nnamespace lexyacc {\\n\\n&@" -e "$ s@.*@&\\n}}\\n@" ${CMAKE_CURRENT_BINARY_DIR}/lex_lexer.cpp.temp > ${CMAKE_CURRENT_BINARY_DIR}/lex_lexer.cpp
--- rod.

I don't see them!

theNbomr 04-15-2011 10:26 AM

Oops, my bad. I guess I miscounted them when I first looked.
Does your sed command work as specified, but independently of make? Does the input file exist? Non-empty? Where its supposed to be when make runs?

--- rod.

MTK358 04-15-2011 11:31 AM

Quote:

Originally Posted by theNbomr (Post 4325553)
Oops, my bad. I guess I miscounted them when I first looked.
Does your sed command work as specified, but independently of make? Does the input file exist? Non-empty? Where its supposed to be when make runs?

--- rod.

That's kind of irrelavent becasue when you look at the error messages, it's /bin/sh, not sed, throwing the error.

I think that the biggest problem is that I don't understand how the command is executed by CMake. Is it passed unmodified to bash? Does CMake interpret certain quotes and escape characters?

I thought that CMake interprets them becasue Kate doesn't highlight single quotes, and even if I do try them, CMake interprets the '#' characters as the start of a comment. And then bash says that there's an unmatched single quote.

MTK358 04-16-2011 11:29 AM

I found that CMake inserts a backslash before every space in the Makefile. Why is that, and how to fix it?

CMakeLists.txt:

Code:

${SED_TOOL} -e "'1 s@.*@\#include \"config.hpp\"\\nnamespace LANG_NAMESPACE {\\nnamespace lexyacc {\\n\\n&@'" -e "'\$ s@.*@&\\n}}\\n@'"
generated makefile:

Code:

/bin/sed -e '1\ s@.*@#include\ "config.hpp"\nnamespace\ LANG_NAMESPACE\ {\nnamespace\ lexyacc\ {\n\n&@' -e '$\ s@.*@&\n}}\n@'


All times are GMT -5. The time now is 05:27 AM.