Browse Source

Writer writes to sane place, RunProcess WIP

ModuleSystem
Macoy Madson 10 months ago
parent
commit
b85789bb32
7 changed files with 131 additions and 8 deletions
  1. +6
    -1
      .gitignore
  2. +3
    -1
      Jamrules
  3. +4
    -1
      src/Jamfile
  4. +3
    -1
      src/Main.cpp
  5. +74
    -0
      src/RunProcess.cpp
  6. +34
    -3
      src/Writer.cpp
  7. +7
    -1
      src/Writer.hpp

+ 6
- 1
.gitignore View File

@ -30,4 +30,9 @@
TAGS
# Ignore executable
cakelisp
cakelisp
src/dynamicLoadTest
src/runProcessTest
*.cake.cpp
*.cake.hpp

+ 3
- 1
Jamrules View File

@ -21,7 +21,9 @@ C++FLAGS = -std=c++11 -Wall -Wextra -Wno-unused-parameter
# BT_USE_DOUBLE_PRECISION solves the Dantzig LCP missing definition
# Disabled now that I'm compiling Bullet in single-precision
# -DBT_USE_DOUBLE_PRECISION
-g ;
-g
-DUNIX
;
# HDRS = src
# # Dependencies/base2.0


+ 4
- 1
src/Jamfile View File

@ -6,8 +6,11 @@ Converters.cpp
Writer.cpp
;
MakeLocate cakelisp : ../cakelisp ;
Main dynamicLoadTest : DynamicLoader.cpp ;
MakeLocate cakelisp : ../cakelisp ;
Main runProcessTest : RunProcess.cpp ;
# C++FLAGS on src/RunProcess.o = -DUNIX ;
SubDir . src ;

+ 3
- 1
src/Main.cpp View File

@ -126,10 +126,12 @@ int main(int argc, char* argv[])
{
NameStyleSettings nameSettings;
WriterFormatSettings formatSettings;
WriterOutputSettings outputSettings;
outputSettings.sourceCakelispFilename = filename;
printf("\nResult:\n");
printGeneratorOutput(generatedOutput, nameSettings, formatSettings);
printGeneratorOutput(generatedOutput, nameSettings, formatSettings, outputSettings);
}
environmentDestroyMacroExpansionsInvalidateTokens(environment);


+ 74
- 0
src/RunProcess.cpp View File

@ -0,0 +1,74 @@
#include <stdio.h>
#ifdef UNIX
#include <sys/types.h> // pid
#include <sys/wait.h> // waitpid
#include <unistd.h> // exec, fork
#include <string.h>
#endif
#include "Utilities.hpp"
// Never returns, if success
void systemExecute()
{
#ifdef UNIX
// pid_t pid;
char fileToExec[MAX_PATH_LENGTH] = {0};
PrintBuffer(fileToExec, "/usr/bin/clang++");
// PrintBuffer(fileToExec, "/usr/bin/ls");
// char arg0[64] = {0};
// PrintBuffer(arg0, "--version");
// If not null terminated, the call will fail
char* arguments[] = {fileToExec, strdup("--version"), nullptr};
printf("Running %s\n", fileToExec);
execvp(fileToExec, arguments);
perror("RunProcess execvp() error: ");
printf("Failed to execute %s\n", fileToExec);
#endif
}
int main()
{
#ifdef UNIX
int pipeFileDescriptors[2] = {0};
const int PipeRead = 0;
const int PipeWrite = 1;
if (pipe(pipeFileDescriptors) == -1)
{
perror("RunProcess: ");
return 1;
}
pid_t pid = fork();
if (pid == -1)
{
perror("RunProcess fork() error: cannot create child");
return 1;
}
// Child
else if (pid == 0)
{
// Only write
close(pipeFileDescriptors[PipeRead]);
systemExecute();
// A failed child should not flush parent files
_exit(EXIT_FAILURE); /* */
}
// Parent
else
{
// Only read
close(pipeFileDescriptors[PipeWrite]);
printf("Created child process %d\n", pid);
int status;
waitpid(pid, &status, 0);
return status;
}
return 1;
#endif
return 0;
}

+ 34
- 3
src/Writer.cpp View File

@ -222,28 +222,59 @@ FILE* fileOpen(const char* filename)
void printGeneratorOutput(const GeneratorOutput& generatedOutput,
const NameStyleSettings& nameSettings,
const WriterFormatSettings& formatSettings)
const WriterFormatSettings& formatSettings,
const WriterOutputSettings& outputSettings)
{
printf("\tTo source file:\n");
{
StringOutputState sourceState = {};
sourceState.fileOut = fileOpen("Test.cpp");
if (outputSettings.sourceCakelispFilename)
{
char generatedSourceFilename[MAX_PATH_LENGTH] = {0};
PrintfBuffer(generatedSourceFilename, "%s.cpp", outputSettings.sourceCakelispFilename);
sourceState.fileOut = fileOpen(generatedSourceFilename);
}
for (const StringOutput& operation : generatedOutput.source)
{
// Debug print mapping
if (!operation.output.empty())
printf("%s %d\n", operation.output.c_str(), sourceState.numCharsOutput + 1);
{
printf("%s \t%d\tline %d\n", operation.output.c_str(),
sourceState.numCharsOutput + 1, sourceState.currentLine + 1);
}
printStringOutput(nameSettings, formatSettings, operation, sourceState);
}
printf("Wrote %d characters\n", sourceState.numCharsOutput);
if (sourceState.fileOut)
{
fclose(sourceState.fileOut);
sourceState.fileOut = nullptr;
}
}
printf("\n\tTo header file:\n");
{
StringOutputState headerState = {};
if (outputSettings.sourceCakelispFilename)
{
char generatedSourceFilename[MAX_PATH_LENGTH] = {0};
PrintfBuffer(generatedSourceFilename, "%s.hpp", outputSettings.sourceCakelispFilename);
headerState.fileOut = fileOpen(generatedSourceFilename);
}
for (const StringOutput& operation : generatedOutput.header)
{
printStringOutput(nameSettings, formatSettings, operation, headerState);
}
if (headerState.fileOut)
{
fclose(headerState.fileOut);
headerState.fileOut = nullptr;
}
}
// Metadata


+ 7
- 1
src/Writer.hpp View File

@ -17,8 +17,14 @@ struct WriterFormatSettings
int indentTabWidth = 2;
};
struct WriterOutputSettings
{
const char* sourceCakelispFilename;
};
const char* importLanguageToString(ImportLanguage type);
void printGeneratorOutput(const GeneratorOutput& generatedOutput,
const NameStyleSettings& nameSettings,
const WriterFormatSettings& formatSettings);
const WriterFormatSettings& formatSettings,
const WriterOutputSettings& outputSettings);

Loading…
Cancel
Save