%%%-----------------------------------------------------------
%%% Copyright Christian Arnault LAL-Orsay CNRS
%%% arnault@lal.in2p3.fr
%%% See the complete license in cmt_license.txt "http://www.cecill.info". 
%%%-----------------------------------------------------------

%%%---------------------------------
%%%------ demoA --------------------
%%%---------------------------------
%%package demoA

branches classes demoA

application demoA demoA.cxx foo=bar -target_tag
application A A.java 

pattern -global multi macro m1 "" Linux m1linux alpha m1alpha ; macro m2 m2

macro m3 m3

make_fragment rename -header=rename_header

document rename XXX OFFSET=XXX demoA.cxx

application XXXdemoA -check -group=X XXXdemoA.cxx

include_path $(demoA_root)

setup_script init

%%%---------------------------------
%%file cmt/init.bat

echo "doing A init"

%%%---------------------------------
%%file cmt/init.csh

echo "doing A init"

%%%---------------------------------
%%file cmt/init.sh

echo "doing A init"

%%%---------------------------------
%%file src/demoA.cxx
#include <stdio.h>
 
int main ()
{
  //xxx
  printf ("demoA\n");

  return (0);
}

%%%---------------------------------
%%file demoA/demoA.h

#define demoA 123

%%%---------------------------------
%%file src/A.java

public class A
{
  public static void main (String arg[])
  {
    System.out.println ("A");
  }
}

%%%---------------------------------
%%file cmt/fragments/rename

## ${SUFFIX}

${CONSTITUENT} :: ${FILEPATH}${OFFSET}${NAME}${FILESUFFIX}


${FILEPATH}${OFFSET}${NAME}${FILESUFFIX} : ${FULLNAME}
	cp $? $@

clean::
	/bin/rm -f ${FILEPATH}${OFFSET}${NAME}${FILESUFFIX}

%%%---------------------------------
%%file cmt/fragments/rename_header

%%%---------------------------------
%%file cmt/fragments/nmake/rename

## ${SUFFIX}

${CONSTITUENT} :: ${FILEPATH}${OFFSET}${NAME}${FILESUFFIX}

${FILEPATH}${OFFSET}${NAME}${FILESUFFIX} : ${FULLNAME}
	copy $? $@

clean::
	del ${FILEPATH}${OFFSET}${NAME}${FILESUFFIX}

%%%---------------------------------
%%file cmt/fragments/nmake/rename_header

%%%---------------------------------
%%%------ demoB --------------------
%%%---------------------------------
%%package demoB
package demoB

use demoA

application demoB -check demoB.cxx

#application B B.java

set before_init "before_init"

setup_script init

set after_init "after_init"

%%file cmt/init.bat

echo "doing B init"

%%file cmt/init.csh

echo "doing B init"

%%file cmt/init.sh

echo "doing B init"

%%file src/demoB.cxx
#include <stdio.h>

#include <demoA/demoA.h>
 
int main ()
{
  printf ("demoB demoA=%d\n", demoA);

  return (0);
}

%%file src/B.java

public class B
{
  public static void main (String arg[])
  {
    System.out.println ("B");
  }
}

%%%---------------------------------
%%%------ demoC --------------------
%%%---------------------------------
%%package demoC
package demoC

build_strategy rebuild_makefiles

include_path ../demoC

branches demoC

library C C*.cxx

macro demoC_linkopts "-L$(DEMOCROOT)/$(demoC_tag) -lC" \
      WIN32          ""

application demoC -check demoC.cxx

path_append LD_LIBRARY_PATH "" \
            Linux "../$(demoC_tag)"


%%file src/demoC.cxx
#include <stdio.h>

typedef void (*Procedure) ();

#ifdef WIN32
#include <windows.h>

Procedure get_proc (const char* name)
{
  void* f = ::LoadLibrary ("..\\Win32Debug\\C.dll");

  if (f == 0)
    {
      f = ::LoadLibrary ("..\\VisualC\\C.dll");
    }

  if (f == 0)
    {
      printf ("Cannot load the library\n");
      exit (0);
    }

  Procedure p;

  p = (Procedure)::GetProcAddress ((HMODULE) f, name);
  if (p == 0)
    {
      char* s = new char[strlen(name) + 2];
      s[0] = '_';
      strcpy (s+1, name);
      p = (Procedure)::GetProcAddress((HMODULE) f, s);
    } 

  if (p == 0)
    {
      printf ("Cannot get the module %s\n", name);
      exit (0);
    }

  return (p);
}
#else
extern "C" {
void C1 ();
void C2 ();
void C3 ();
}
#endif

int main ()
{
  printf ("demoC\n");

#ifdef WIN32

  Procedure c = 0;

  c = get_proc ("C1");
  c ();
  c = get_proc ("C2");
  c ();
  c = get_proc ("C3");
  c ();
#else
  C1 ();
  C2 ();
  C3 ();
#endif

  return (0);
}

%%file src/C1.cxx
#include <stdio.h>

extern "C" { 
  void C1 ();
}

void C1 ()
{
  printf ("C1\n");
}

%%file src/C2.cxx
#include <stdio.h>
 
extern "C" { 
  void C2 ();
}

void C2 ()
{
  printf ("C2\n");
}

%%file src/C3.cxx
#include <stdio.h>
 
extern "C" { 
  void C3 ();
}

void C3 ()
{
  printf ("C3\n");
}

%%%---------------------------------
%%%------ Actions ------------------
%%%---------------------------------
%%cdpackage demoA
%%cmt run "gmake all_groups"
%%%cmt run "gmake check"
%%cdpackage demoB
%%%cmt run "gmake all"
%%%cmt run "gmake check"
%%cdpackage demoC
%%%cmt run "cmt config"
%%%cmt run ". setup.sh ; gmake QUIET=1 all check"
