af7b09259a2bf7520f758bc4e6a2248860f04b0f
[platform/upstream/cmake.git] / Tests / LoadCommandOneConfig / CMakeCommands / cmTestCommand.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "cmCPluginAPI.h"
6
7 typedef struct
8 {
9   char* LibraryName;
10   int Argc;
11   char** Argv;
12 } cmVTKWrapTclData;
13
14 /* do almost everything in the initial pass */
15 static int CCONV InitialPass(void* inf, void* mf, int argc, char* argv[])
16 {
17   char* file;
18   char* str;
19   char* srcs;
20   const char* cstr;
21   char buffer[1024];
22   void* source_file;
23   char* args[2];
24   char* ccArgs[4];
25   char* ccDep[1];
26   char* ccOut[1];
27   cmLoadedCommandInfo* info = (cmLoadedCommandInfo*)inf;
28
29   cmVTKWrapTclData* cdata =
30     (cmVTKWrapTclData*)malloc(sizeof(cmVTKWrapTclData));
31   cdata->LibraryName = "BOO";
32   cdata->Argc = argc;
33   cdata->Argv = argv;
34   info->CAPI->SetClientData(info, cdata);
35
36   /* Now check and see if the value has been stored in the cache */
37   /* already, if so use that value and don't look for the program */
38   if (!info->CAPI->IsOn(mf, "TEST_COMMAND_TEST1")) {
39     info->CAPI->AddDefinition(mf, "TEST_DEF", "HOO");
40     return 1;
41   }
42
43   info->CAPI->AddDefinition(mf, "TEST_DEF", "HOO");
44   cdata->LibraryName = "HOO";
45
46   info->CAPI->AddCacheDefinition(mf, "SOME_CACHE_VARIABLE", "ON",
47                                  "Test cache variable", CM_CACHE_BOOL);
48   info->CAPI->AddCacheDefinition(mf, "SOME_CACHE_VARIABLE1", "",
49                                  "Test cache variable 1", CM_CACHE_PATH);
50   info->CAPI->AddCacheDefinition(mf, "SOME_CACHE_VARIABLE2", "",
51                                  "Test cache variable 2", CM_CACHE_FILEPATH);
52   info->CAPI->AddCacheDefinition(mf, "SOME_CACHE_VARIABLE3", "",
53                                  "Test cache variable 3", CM_CACHE_STRING);
54   info->CAPI->AddCacheDefinition(mf, "SOME_CACHE_VARIABLE4", "",
55                                  "Test cache variable 4", CM_CACHE_INTERNAL);
56   info->CAPI->AddCacheDefinition(mf, "SOME_CACHE_VARIABLE5", "",
57                                  "Test cache variable 5", CM_CACHE_STATIC);
58
59   file = info->CAPI->ExpandVariablesInString(mf, "${CMAKE_COMMAND}", 0, 0);
60
61   str = info->CAPI->GetFilenameWithoutExtension(file);
62   info->CAPI->DisplaySatus(mf, str);
63   info->CAPI->Free(str);
64   str = info->CAPI->GetFilenamePath(file);
65   info->CAPI->DisplaySatus(mf, str);
66   info->CAPI->Free(str);
67   str = info->CAPI->Capitalized("cmake");
68   info->CAPI->DisplaySatus(mf, str);
69   info->CAPI->Free(str);
70
71   info->CAPI->DisplaySatus(mf, info->CAPI->GetProjectName(mf));
72   info->CAPI->DisplaySatus(mf, info->CAPI->GetHomeDirectory(mf));
73   info->CAPI->DisplaySatus(mf, info->CAPI->GetHomeOutputDirectory(mf));
74   info->CAPI->DisplaySatus(mf, info->CAPI->GetStartDirectory(mf));
75   info->CAPI->DisplaySatus(mf, info->CAPI->GetStartOutputDirectory(mf));
76   info->CAPI->DisplaySatus(mf, info->CAPI->GetCurrentDirectory(mf));
77   info->CAPI->DisplaySatus(mf, info->CAPI->GetCurrentOutputDirectory(mf));
78   sprintf(buffer, "Cache version: %d.%d, CMake version: %d.%d",
79           info->CAPI->GetCacheMajorVersion(mf),
80           info->CAPI->GetCacheMinorVersion(mf),
81           info->CAPI->GetMajorVersion(mf), info->CAPI->GetMinorVersion(mf));
82   info->CAPI->DisplaySatus(mf, buffer);
83   if (info->CAPI->CommandExists(mf, "SET")) {
84     info->CAPI->DisplaySatus(mf, "Command SET exists");
85   }
86   if (info->CAPI->CommandExists(mf, "SET_FOO_BAR")) {
87     info->CAPI->SetError(mf, "Command SET_FOO_BAR should not exists");
88     return 0;
89   }
90   info->CAPI->AddDefineFlag(mf, "-DADDED_DEFINITION");
91
92   source_file = info->CAPI->CreateNewSourceFile(mf);
93   cstr = info->CAPI->SourceFileGetSourceName(source_file);
94   sprintf(buffer, "Should be empty (source file name): [%s]", cstr);
95   info->CAPI->DisplaySatus(mf, buffer);
96   cstr = info->CAPI->SourceFileGetFullPath(source_file);
97   sprintf(buffer, "Should be empty (source file full path): [%s]", cstr);
98   info->CAPI->DisplaySatus(mf, buffer);
99   info->CAPI->DefineSourceFileProperty(mf, "SOME_PROPERTY", "unused old prop",
100                                        "This property is no longer used", 0);
101   if (info->CAPI->SourceFileGetPropertyAsBool(source_file, "SOME_PROPERTY")) {
102     info->CAPI->SetError(mf, "Property SOME_PROPERTY should not be defined");
103     return 0;
104   }
105   info->CAPI->DefineSourceFileProperty(mf, "SOME_PROPERTY2", "nice prop",
106                                        "This property is for testing.", 0);
107   info->CAPI->SourceFileSetProperty(source_file, "SOME_PROPERTY2", "HERE");
108   cstr = info->CAPI->SourceFileGetProperty(source_file, "ABSTRACT");
109   sprintf(buffer, "Should be 0 (source file abstract property): [%p]", cstr);
110   info->CAPI->DisplaySatus(mf, buffer);
111
112   info->CAPI->DestroySourceFile(source_file);
113
114   srcs = argv[2];
115   info->CAPI->AddExecutable(mf, "LoadedCommand", 1, &srcs, 0);
116
117   /* add customs commands to generate the source file */
118   ccArgs[0] = "-E";
119   ccArgs[1] = "copy";
120   ccArgs[2] = argv[0];
121   ccArgs[3] = argv[1];
122   ccDep[0] = ccArgs[2];
123   ccOut[0] = ccArgs[3];
124   info->CAPI->AddCustomCommand(mf, "LoadedCommand.cxx.in", file, 4, ccArgs, 1,
125                                ccDep, 1, ccOut, "LoadedCommand");
126
127   ccArgs[2] = argv[1];
128   ccArgs[3] = argv[2];
129   ccDep[0] = ccArgs[2];
130   ccOut[0] = ccArgs[3];
131   info->CAPI->AddCustomCommandToOutput(mf, ccOut[0], file, 4, ccArgs, ccDep[0],
132                                        0, 0);
133
134   ccArgs[1] = "echo";
135   ccArgs[2] = "Build has finished";
136   info->CAPI->AddCustomCommandToTarget(mf, "LoadedCommand", file, 3, ccArgs,
137                                        CM_POST_BUILD);
138
139   info->CAPI->Free(file);
140
141   args[0] = "TEST_EXEC";
142   args[1] = "TRUE";
143
144   /* code coverage */
145   if (info->CAPI->GetTotalArgumentSize(2, args) != 13) {
146     return 0;
147   }
148   info->CAPI->ExecuteCommand(mf, "SET", 2, args);
149
150   /* make sure we can find the source file */
151   if (!info->CAPI->GetSource(mf, argv[1])) {
152     info->CAPI->SetError(mf, "Source file could not be found!");
153     return 0;
154   }
155
156   return 1;
157 }
158
159 static void CCONV FinalPass(void* inf, void* mf)
160 {
161   cmLoadedCommandInfo* info = (cmLoadedCommandInfo*)inf;
162   /* get our client data from initial pass */
163   cmVTKWrapTclData* cdata = (cmVTKWrapTclData*)info->CAPI->GetClientData(info);
164   if (strcmp(info->CAPI->GetDefinition(mf, "TEST_DEF"), "HOO") ||
165       strcmp(cdata->LibraryName, "HOO")) {
166     fprintf(stderr, "*** Failed LOADED COMMAND Final Pass\n");
167   }
168 }
169 static void CCONV Destructor(void* inf)
170 {
171   cmLoadedCommandInfo* info = (cmLoadedCommandInfo*)inf;
172   /* get our client data from initial pass */
173   cmVTKWrapTclData* cdata = (cmVTKWrapTclData*)info->CAPI->GetClientData(info);
174   free(cdata);
175 }
176
177 #ifdef MUCHO_MUDSLIDE
178 void CM_PLUGIN_EXPORT CCONV CMAKE_TEST_COMMANDInit(cmLoadedCommandInfo* info)
179 {
180   info->InitialPass = InitialPass;
181   info->FinalPass = FinalPass;
182   info->Destructor = Destructor;
183   info->m_Inherited = 0;
184   info->Name = "CMAKE_TEST_COMMAND";
185 }
186 #endif