[Cmake] How to use info->CAPI->AddDefinition ?

Jean-Francois LECOMTE lecomte at litteral . fr
31 Oct 2003 11:58:45 +0100


--=-UN1eR/UudYy6umvAsF39
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi there,

I'm writing a plugin for cmake and i 'm able to create it.

Everything seems ok, that means i can use it in CMakeLists.txt for my project.

In my static InitialPass method i write

info->CAPI->AddDefinition( mf, "FOO", "bar" );
and
info->CAPI->AddDefineFlag( mf, "-DTUTU");

the second gives me the expected  flag -DTUTU in my current Makefile, the first nothing....
I really don't understand why.

Does anyone already face the same pb.
Thanks...

Full code of my plugin is given as attachement 

--=-UN1eR/UudYy6umvAsF39
Content-Disposition: attachment; filename=cmWrapXwi.c
Content-Type: text/x-c; name=cmWrapXwi.c; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit

#include <stdio.h>
#include <stdlib.h>
#include "cmCPluginAPI.h"

typedef struct _XwiClientData {
  int    size;
  int    NWrapFiles;
  void** WrapFiles;
} XwiClientData;

XwiClientData *XwiClientDataNew(int size)
{
  XwiClientData* xcd = (XwiClientData*)malloc( sizeof(XwiClientData) );
  xcd->size       = size;
  xcd->WrapFiles  = (void**)malloc( xcd->size * sizeof(void*) );
  xcd->NWrapFiles = 0;

  return xcd;
}

void XwiClientDataDelete( XwiClientData* xcd )
{
  if (xcd)
    {
      if (xcd->WrapFiles)
	free( xcd->WrapFiles );
      free(xcd);
    }
}

void XwiClientDataAddFile( XwiClientData* xcd, void* source_file )
{
  if ( xcd->NWrapFiles == xcd->size )
    {
      xcd->size += 10;
      xcd->WrapFiles = (void**)realloc( xcd->WrapFiles, xcd->size * sizeof(void*) );
    }

  xcd->WrapFiles[ xcd->NWrapFiles ] = source_file;
  xcd->NWrapFiles++;

}


static int InitialPass (void *inf, void *mf, int argc, char **argv) 
{
  int i;
  int nfiles;
  char **files;
  const char* WRAP_XWI_value;
  const char* cdir;
  char* library_name;
  char* source_list;
  int   source_list_value_len = 0;
  char* source_list_value;
  char* def;
  XwiClientData* xcd;

  cmLoadedCommandInfo* info = (cmLoadedCommandInfo*)inf;
  if( argc < 3 )
    {
    info->CAPI->SetError( info, "called with incorrect number of arguments" );
    return 0;
    }

  info->CAPI->ExpandSourceListArguments( mf, argc, (const char**)argv, &nfiles, &files, 2 );
  
  /* Now check and see if the value has been stored in the cache
     already, if so use that value and don't look for the program  */
  WRAP_XWI_value = info->CAPI->GetDefinition( mf, "WRAP_XWI");
  if (WRAP_XWI_value == 0)
    {
    info->CAPI->SetError(info, "called with WRAP_XWI undefined");
    return 0;
    }

  
  cdir = info->CAPI->GetCurrentDirectory( mf );
  library_name = files[0];
  source_list  = files[1];

  /* was the list already populated */
  def = (char *)info->CAPI->GetDefinition( mf, source_list );
  if (def)
    {
      source_list_value_len =  strlen(def);
      source_list_value = (char*)malloc( (source_list_value_len + 1)*sizeof(char) );
      strcpy( source_list_value, def );
    }

  xcd = XwiClientDataNew( nfiles - 2 );

  for ( i=2; i< nfiles; i++)
    {
      char *src_name;
      void* cm_file;
      void* cm_curr_file;

      cm_curr_file = info->CAPI->GetSource( mf, files[i] );
      
      if ( !cm_curr_file || !info->CAPI->SourceFileGetPropertyAsBool( cm_curr_file, "WRAP_EXCLUDE" ) )
	{
	  char* full_name;
	  void* cm_file = info->CAPI->CreateSourceFile();	  
	  
	  if (cm_curr_file)
	    info->CAPI->SourceFileSetProperty( cm_file, "ABSTRACT", info->CAPI->SourceFileGetProperty(cm_curr_file, "ABSTRACT" ) ) ;
	  
	  src_name = info->CAPI->GetFilenameWithoutExtension( files[i] );

	  printf("src_name %s\n", src_name );

	  full_name = (char*)malloc( (strlen( cdir) + strlen( files[i] ) + 2)*sizeof(char) );
	  sprintf( full_name, "%s/%s", cdir, files[i] );

	  printf("full_name %s\n", full_name );

	  info->CAPI->SourceFileSetName2( cm_file, src_name, info->CAPI->GetCurrentOutputDirectory( mf ), "c", 0 );
	  info->CAPI->SourceFileAddDepend( cm_file, full_name );

	  XwiClientDataAddFile( xcd, cm_file );

	  if (source_list_value_len == 0)
	    {
	      source_list_value_len = ( strlen(src_name) + 2 /*.c*/ + 1 ) * sizeof(char); ;
	      source_list_value = (char*)malloc( source_list_value_len );
	      sprintf(source_list_value, "%s%s", src_name, ".c" );
	    }
	  else
	    {
	      source_list_value_len += ( 1 /*;*/ + strlen(src_name) + 2 /*.c*/ ) * sizeof(char); ;
	      source_list_value = (char*)realloc( source_list_value,  source_list_value_len );
	      strcat( source_list_value, ";" );
	      strcat( source_list_value, src_name);
	      strcat( source_list_value, ".c" );
	    }

	  printf(" source_list_value %s\n", source_list_value );
	  free(full_name);
	}
    }
      
  info->CAPI->SetClientData( info, (void*)xcd );
  
  info->CAPI->AddDefinition ( mf, source_list, source_list_value );

  return 1;

}


static void
FinalPass (void *inf, void *mf)
{
  int i;
  cmLoadedCommandInfo* info = (cmLoadedCommandInfo*)inf;
  XwiClientData* xcd = (XwiClientData*)info->CAPI->GetClientData(info);

  for (i =0; i< xcd->NWrapFiles; ++i)
    {
      info->CAPI->AddSource( mf, xcd->WrapFiles[i] );
      printf(" Source %s\n", info->CAPI->SourceFileGetSourceName( xcd->WrapFiles[i] ));
    }
  
 
}





void CM_PLUGIN_EXPORT
WRAP_XWIInit(cmLoadedCommandInfo* info)
{
  info->InitialPass = InitialPass;
  info->FinalPass   = FinalPass;
  info->m_Inherited = 1;
  info->Name        = "WRAP_XWI";
}

--=-UN1eR/UudYy6umvAsF39--