Automating slice2cpp using Qt4

ctennisctennis Caleb TennisOrganization: AEIProject: Project GINAMember ✭✭✭
Attached is a small piece of code I've developed that allows you to use Qt's qmake system for your project and integrate ice files into it as well. Put this into a .pri file (I call mine ice3.pri) and include it in your pro file (e.g., include(ice3.pri) ).

This adds a qmake function called addIceFiles where you can specify one or more ice files to add to your project. slice2cpp will automatically be run on these files when they need to be regenerated. The resulting .cpp and .h files will be placed into a directory called .ice in your current project's directory, and they will be added to your project's SOURCES and HEADERS variables.

You need to define the variable ICE_SLICEDIR if you need to make use of any of the existing .ice files provided by Zeroc. As well, you can define the variable SLICE_OPTIONS to pass any extra options to the slice2cpp compiler.

Anyway, feel free to use this code on your Qt4 projects. It certainly can be improved upon, but I thought it might be helpful to some folks out there.
isEmpty(ADD_ICE_FILES_DEFINED) {

ADD_ICE_FILES_DEFINED=true
defineTest(addIceFiles) {

  ICE_FILES = $$ARGS
  !exists($$ICE_FILES) { 
    error("No ICE_FILES defined") 
    return false;
  }

  isEmpty(ICE_FILE_DIR) { ICE_FILE_DIR = . }
  ICE_OUT_DIR = $$OUT_PWD/.ice
  !exists( $$ICE_OUT_DIR ) { system(mkdir -p $$ICE_OUT_DIR) }

   MY_ICE_FILES = $$join(ICE_FILES, " $$ICE_DIR/", $$ICE_DIR/)

   for(icefile, MY_ICE_FILES) {

     # Don't reparse it if we've already got it in our list
     contains(ALREADY_PARSED_ICE_FILES, $$icefile) {
       next()
     }

     OUTCPPF = $$icefile
     OUTCPPF ~= s/\.ice/.cpp/g
     OUTINCF = $$icefile
     OUTINCF ~= s/\.ice/.h/g

     BASECPPFILE = $$basename(OUTCPPF)
     BASEINCFILE = $$basename(OUTINCF)

     SOURCES += $$join(ICE_OUT_DIR , "", "", "/$$basename(OUTCPPF)" )
     HEADERS += $$join(ICE_OUT_DIR , "", "", "/$$basename(OUTINCF)" )

     !exists($$ICE_OUT_DIR/$$BASECPPFILE) {
       system(slice2cpp -I/g4/ice -I$$ICE_SLICEDIR $$SLICE_OPTIONS $$icefile --output-dir $$ICE_OUT_DIR)
     }

     eval(ice_$${OUTCPPF}.commands = @slice2cpp -I/g4/ice -I$$ICE_SLICEDIR $$SLICE_OPTIONS $$icefile --output-dir $$ICE_OUT_DIR)
     eval(ice_$${OUTCPPF}.depends = $$icefile)
     eval(ice_$${OUTCPPF}.target = .ice/$$BASECPPFILE)
     export(ice_$${OUTCPPF}.commands)
     export(ice_$${OUTCPPF}.depends)
     export(ice_$${OUTCPPF}.target)

     eval(ice_$${OUTINCF}.commands = @slice2cpp -I/g4/ice -I$$ICE_SLICEDIR $$SLICE_OPTIONS $$icefile --output-dir $$ICE_OUT_DIR)
     eval(ice_$${OUTINCF}.depends = $$icefile)
     eval(ice_$${OUTINCF}.target = .ice/$$BASEINCFILE)
     export(ice_$${OUTINCF}.commands)
     export(ice_$${OUTINCF}.depends)
     export(ice_$${OUTINCF}.target)

     QMAKE_EXTRA_TARGETS += ice_$${OUTCPPF} ice_$${OUTINCF}
     export(QMAKE_EXTRA_TARGETS)
     PRE_TARGETDEPS += $$icefile
     export(PRE_TARGETDEPS)

     ALREADY_PARSED_ICE_FILES += $$icefile
     export(ALREADY_PARSED_ICE_FILES)

   }

  export(HEADERS)
  export(SOURCES)

  INCLUDEPATH *= $$ICE_OUT_DIR

  export(INCLUDEPATH)

  !isEmpty(ICE_FILE_INSTALL) {
    icefile.path = $$ICE_FILE_INSTALL
    icefile.files += $$MY_ICE_FILES
    INSTALLS *= icefile
  }

  return(true)
}

}
Sign In or Register to comment.