News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

Pass argument to Build Scripts

Started by Feneck91, June 21, 2012, 11:31:22 AM

Previous topic - Next topic

Feneck91

For a database project I need to generate cpp + h files from a ws3 file (tables descriptions for sqlite3) with an external software : lua (is cross platform script engine, I could run it on several platforms).
To compile it, only set properties of ws3 file like :

  • In Build tab: compile file checked
  • In Build tab: set the priority weight to 25 (compile before other files)
  • In Advanced tab: check "use custom command to build this file:"
  • In Advanced tab: set the build command as "$(#wxetksqlite3)/build/Win32/lua52.exe" "$(#wxetksqlite3)/scripts/compile_ws3.lua" $file qt $file_name.h $file_name.cpp"
  • Add cpp and header file to the project

It is a little bit complex. I could also make a new compiler in charge to build this kind of file, but when changing dev platform I must re-create it each time.
To simplifying this and don't make mistakes, only add the next script into Project/target options, tab Attached build scripts: the script as
Quote$(#wxetksqlite3)\scripts\compile_ws3.script

The script is:
Quote/**
* Add to Win32 :  $(#wxetksqlite3)\scripts\compile_ws3.script Win32
* Add to Linux :  $(#wxetksqlite3)\scripts\compile_ws3.script Linux
* Add to MaxOSX : $(#wxetksqlite3)\scripts\compile_ws3.script MacOSX
*/
//local g_strPlatform;
//
//LogError(_T("a")); // Log to Code::Blocks log panel
//
//if ($# != 1)
//{
//    LogError(_T("c")); // Log to Code::Blocks log panel
//    LogError(_T("compile_ws3.script: missing platform argument (Win32 / Linux / MacOSX)!")); // Log to Code::Blocks log panel
//}
//else
//{
//    LogError(_T("b")); // Log to Code::Blocks log panel
//    LogError(_T("Argument = ") + __argv[0] + _T(" -- ") + __argv[1]);
//
//}

/**
* Generate files source files from ws3 description.
*
* This script is called before the project/target is built, Each build script must define
* this function, even if it is not needed (in which case you can leave it with an empty body).
*
* Help can be found here:
*  - Base page:          http://wiki.codeblocks.org/index.php?title=Scripting_Code::Blocks<br>
*  - Scripting commands: http://wiki.codeblocks.org/index.php?title=Scripting_commands<br>
*  - Build scripts:      http://wiki.codeblocks.org/index.php?title=Build_scripts<br>
*
* @param pCompileTargetBase CompileTargetBase instance.
*/
function SetBuildOptions(pCompileTargetBase)
{
//    LogWarning(_T("---------------------------------------------"));
//    LogWarning(_T("--           SetBuildOptions               --"));
//    LogWarning(_T("---------------------------------------------"));

    local cbProject = GetProjectManager().IsOpen(pCompileTargetBase.GetFilename())
    if (!IsNull(cbProject))
    {
        local i = 0;
        for (i=0;i<cbProject.GetFilesCount();i++)
        {
            if (cbProject.GetFile(i).file.GetExt().tostring() == "ws3")
            {   // Generate match file
                local pFileWS3 = cbProject.GetFile(i);
                if (ConfigureWS3Compilation(cbProject,cbProject.GetFile(i)))
                {
                    pFileWS3.compile = true;
                    pFileWS3.weight  = 25;
                    pFileWS3.SetUseCustomBuildCommand(GetCompilerFactory().GetDefaultCompilerID(),true);
                    pFileWS3.SetCustomBuildCommand(GetCompilerFactory().GetDefaultCompilerID(),_T("\"$(#wxetksqlite3)/build/Win32/lua52.exe\" \"$(#wxetksqlite3)/scripts/compile_ws3.lua\" $file qt $file_name.h $file_name.cpp"));
                    cbProject.SetModified(true);
                    GetProjectManager().SaveProject(cbProject);
                }
            }
        }
    }
}

/**
* Detect and add the header / source to project if they doesn't exists.
*
* The source and header have the same name as ws3 files but with h/cpp extension.
*
* @param pProject Project instance, it is the project where the ws3 was found.
* @param pFileName wxFileName instance of file to verify/add to project.
* @return true if the file has been added, false else.
*/
function AddToProjectIfNotExists(pProject,pFileName)
{
    local i = 0;
    local bFound = false;
    local bAdded = false;
    for (i=0;i<pProject.GetFilesCount();i++)
    {
        if (pProject.GetFile(i).file.GetFullPath(wxPATH_NATIVE).tostring() == pFileName.GetFullPath(wxPATH_NATIVE).tostring())
        {   // Don't add the pFileName file
            bFound = true;
            break;
        }
    }
    if (!bFound)
    {   //-----------------------------------------------------------------------------------
        // In : codeblocks\src\include\cbproject.h
        //  Function: ProjectFile* AddFile(AddFile(int targetIndex, const wxString& filename, bool compile = true, bool link = true, unsigned short int weight = 50);
        // AddFile(targetIndex,filename,compile, link,weight): Add a file to the project.
        // @param targetIndex The index of the build target to add this file to.
        // @param filename The file's filename. This *must* be a filename relative to the project's path.
        // @param compile If true this file is compiled when building the project.
        // @param link If true this file is linked when building the project.
        // @param weight A value between zero and 100 (defaults to 50). Smaller weight, makes the file compile earlier than those with larger weight.
        // @return The newly added file or NULL if something went wrong.
        //-----------------------------------------------------------------------------------
        local bCompileAndLink = true;
        if (pFileName.GetExt().tostring() == EXT_H.tostring())
        {
            bCompileAndLink = false;
        }
        // Add the file for all project targets
        local iTargetIndex;
        local pProjectFile = null; // Instance of ProjectFile
        for (iTargetIndex=0;iTargetIndex<pProject.GetBuildTargetsCount();iTargetIndex++)
        {
            if (IsNull(pProjectFile))
            {   // Add the file to the first target
                // Create an empty file
                IO.WriteFileContents(pFileName.GetFullPath(wxPATH_NATIVE),_T("#error This file must be generated!"));
                pProjectFile = pProject.AddFile(iTargetIndex,pFileName.GetFullPath(wxPATH_NATIVE),bCompileAndLink,bCompileAndLink,60);
                if (IsNull(pProjectFile))
                {   // Error, failed to add file
                    break;
                }
                else
                {
                    bAdded = true;
                    Log(_T("Added file '") + pFileName.GetFullPath(wxPATH_NATIVE) + _T("'"));
                    Log(_T("  Added Build Target = '") + pProject.GetBuildTarget(iTargetIndex).GetFullTitle() + _T("'"));
                }
            }
            else
            {   // Add all target to this
                bAdded = true;
                pProjectFile.AddBuildTarget(pProject.GetBuildTarget(iTargetIndex).GetTitle());
                Log(_T("  Added Build Target = '") + pProject.GetBuildTarget(iTargetIndex).GetFullTitle() + _T("'"));
            }
        }

        if (IsNull(pProjectFile))
        {
            Log(_T("Failed on added file '") + pFileName.GetFullPath(wxPATH_NATIVE) + _T("'"));
        }
        else
        {   // Log and save to let the workspace to load and display the changes
            Log(_T("Added file '") + pFileName.GetFullPath(wxPATH_NATIVE));
            pProject.SetModified(true);
            GetProjectManager().SaveProject(pProject);
        }
    }

    return bAdded;
}

/**
* Generate the header / source code from ws3 description file.
*
* @param pProject Project instance, it is the project where the ws3 was found.
* @param pProjectFile ProjectFile instance of ws3 project file. It contains the ws3
*                     file path to read and generate.
* @return true if one of the file has been added, false else.
*/
function ConfigureWS3Compilation(pProject,pProjectFile)
{
    // Would like to put at the beginning of the file as global variable (accessed with ::) but
    // I don't understand the syntax
    local STR_PATH_TEMPLATE_HEADER = "$(#wxetksqlite3)/scripts/wxETKSQLite3TableTemplate.h";
    local STR_PATH_TEMPLATE_SOURCE = "$(#wxetksqlite3)/scripts/wxETKSQLite3TableTemplate.cpp";

    local wxFileNameCPP  = ::wxFileName(); // Create new instance of wxFileName
    local wxFileNameH    = ::wxFileName(); // Create new instance of wxFileName

    // Create 2 file names: cpp and h files
    wxFileNameCPP.Assign(pProjectFile.file.GetFullPath(wxPATH_NATIVE),wxPATH_NATIVE);
    wxFileNameH.Assign(pProjectFile.file.GetFullPath(wxPATH_NATIVE),wxPATH_NATIVE);
    wxFileNameCPP.SetExt(EXT_CPP);
    wxFileNameH.SetExt(EXT_H);

    local bAddCPP = AddToProjectIfNotExists(pProject,wxFileNameCPP);
    local bAddH   = AddToProjectIfNotExists(pProject,wxFileNameH);
    return bAddH || bAddCPP;
}

The problem is: Lua52.exe is into Win32 folder into build directory. But it's work only for Win32. How to pass parameters to the script (Win32 / Linux / MacOSX) to define the Lua exe path to execute ?

oBFusCATed

Have you tried the EnvVar plugin?

You can define LUA_EXE_PATH variable and then you can use $(LUA_EXE_PATH) to launch it.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

carra

You probably won't need this, but just in case: you can also ask for parameters at runtime, for example:

wxGetTextFromUser( _T("Enter build system:"), _T("Build parameters"), _T("WIN32") );

This sets the default as WIN32, so in that case you can just press enter ;)

oBFusCATed

carra: This will get annoying pretty quickly :)
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Feneck91

Quote from: oBFusCATed on June 21, 2012, 11:51:50 AM
Have you tried the EnvVar plugin?

You can define LUA_EXE_PATH variable and then you can use $(LUA_EXE_PATH) to launch it.
I never ear about  EnvVar plugin (variable ?)
Else, your response is what I have done, I define a $(#wxetksqlite3.plateform) with user-defined fields and it's works fine !!!
exactly what I need.
I'm writing my lib in cross plateform and for QT and wxWidgets (based on wxSQLite3, excellent wxWidgets wrapper for sqlite3 library done by Ulrich Telle (http://wxcode.sourceforge.net/components/wxsqlite3/). And I'm working on both Code::Blocks and Microsoft Visual Studio...
When I work on Visual Studio, lot of things are missing (like these variables, mandatory to set environment variables), juste to say 'Code::blocks' is really an excellent IDE, congratulation for all developpers !!!!!