August 21, 2011

RickH RickH
Lab Rat
17 posts

[moved] Adding an extra tool to the tool chain

 

Hi. I’m using QtCreator, and therefore I’m using qmake. I’d like to pre-process my source file with m4 before I run the compiler on them. So in whatever syntax you have to use with qmake, I’d like to state that x.cpp files have a dependency on x.cpp.m4 files, and x.h files have a dependency on x.h.m4 files, and the command to get from one to the other is something like m4 @.h.m4 >@.h. How would I go about saying that?

Regards, Rick

6 replies

August 21, 2011

ZapB ZapB
Robot Herder
1359 posts

Hi, I do something similar to what you need using bison to pre-process the input files. I foudn this technique at http://www.freehackers.org/thomas/2009/11/22/how-to-use-flex-and-bison-with-qmake-my-own-way/

I’ll illustrate using the solution for bison but it shoudl be easily modified to work with m4.

Put the following into a .pri file (bison.pri in my case):

  1. bison.name = Bison ${QMAKE_FILE_IN}
  2. bison.input = BISONSOURCES
  3. bison.output = ${QMAKE_FILE_PATH}/${QMAKE_FILE_BASE}.parser.cpp
  4. win32 {
  5.     bison.commands = bison.exe --defines=${QMAKE_FILE_PATH}/${QMAKE_FILE_BASE}.parser.h -o ${QMAKE_FILE_PATH}/${QMAKE_FILE_BASE}.parser.cpp ${QMAKE_FILE_IN}
  6. } else {
  7.     bison.commands = bison --defines=${QMAKE_FILE_PATH}/${QMAKE_FILE_BASE}.parser.h -o ${QMAKE_FILE_PATH}/${QMAKE_FILE_BASE}.parser.cpp ${QMAKE_FILE_IN}
  8. }
  9. bison.CONFIG += target_predeps
  10. bison.variable_out = GENERATED_SOURCES
  11. silent:bison.commands = @echo Bison ${QMAKE_FILE_IN} && $$bison.commands
  12. QMAKE_EXTRA_COMPILERS += bison

Then in your .pro file you need to include the bison.pri file:

  1. include(bison.pri)

and then specify the BISONSOURCES variable as you would with SOURCES for C++ files:

  1. BISONSOURCES += zcalc.y

As an aside if you then add BISONSOURCES to the OTHER_FILES variable this has the effect of making these sources also show up in Qt-creator’s project view:

  1. OTHER_FILES += $$BISONSOURCES

I think you should be able to substitute bison for m4 in the above to get what you need.

Hope this helps.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

August 22, 2011

RickH RickH
Lab Rat
17 posts

Thanks! I’ll go use this information.

August 30, 2011

RickH RickH
Lab Rat
17 posts

I finally got back to this. My version, very similar to the one above, almost works. The resulting file is consistently placed in the target directory. The directory, parallel to the one that has the source files and header files, that has the object files and executable files. I’d like the results to go into the source directory. So that my file test.hm4 is processed into the file test.h, and further compilation and linking goes on as normal.

I’ve tried a number of variants, but haven’t succeeded in making the file appear in the source directory. Does anyone know how to do that?

August 30, 2011

ZapB ZapB
Robot Herder
1359 posts

Can you post a very minimal trial project that uses what you have so far please so that we can take a look at it and try it for ourselves? It’s difficult to see without it.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

August 30, 2011

RickH RickH
Lab Rat
17 posts

Sure.

test.hm4:

  1. define(declaration,
  2. class saysomething {
  3. public:
  4.     void sayhello(void);
  5.     void saysayanara(void);
  6. };
  7. )
  8.  
  9. declaration

data_struct.pro:

  1. include(m4.pri)
  2.  
  3. QT       += core
  4. QT       -= gui
  5.  
  6. TARGET = data_struct
  7. CONFIG   += console
  8. CONFIG   -= app_bundle
  9.  
  10. TEMPLATE = app
  11.  
  12. SOURCES += main.cpp \
  13.     coreapp.cpp \
  14.     readevalprintloop.cpp
  15.  
  16. HEADERS += \
  17.     coreapp.h \
  18.     readevalprintloop.h
  19.  
  20. HM4SOURCES += test.hm4
  21.  
  22. OTHER_FILES += \
  23.     m4.pri \
  24.     $$HM4SOURCES

m4.pri:

  1. hm4.name = hm4
  2. hm4.input = HM4SOURCES
  3. hm4.output = ${QMAKE_FILE_PATH}/${QMAKE_FILE_BASE}.h
  4. message("Here is the value of QMAKE_FILE_IN")
  5. message(${QMAKE_FILE_IN})
  6. hm4.commands = /m4/m4.exe ${QMAKE_FILE_IN} >${QMAKE_FILE_IN_BASE}.h
  7. hm4.CONFIG = target_predeps
  8. hm4.variable_out = HEADERS
  9. QMAKE_EXTRA_COMPILERS += hm4
  10. # m4.output = $$replace(${QMAKE_FILE_IN}, ".m4", "")
  11. # "Undocumented Qmake" says this doesn't work.  QMAKE_FILE_IN substituted after replace is done.

I hope this is clearer.

August 31, 2011

RickH RickH
Lab Rat
17 posts

I solved my problem, in probably an ugly way. I discovered that Qt Creator offers extra build steps, that you can place before or after the supplied build steps. In case it helps somebody else, here’s the entry I made. Click the Project tab.

Enable custom process step. (checked)
Command: “c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\nmake” -f m4.mak
Working directory: C:\Users\Rick\bazaar_repository\data_struct
Command arguments: (empty)

Rick

 
  ‹‹ Define the installation prefix using qmake?      configure ››

You must log in to post a reply. Not a member yet? Register here!