Skip to content

Commit

Permalink
change to output directory before cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Nov 10, 2023
1 parent ae20cde commit 8b55ab6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
13 changes: 0 additions & 13 deletions w2c2/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
#if HAS_PTHREAD
#include <pthread.h>
#endif /* HAS_PTHREAD */
#if HAS_UNISTD
#include <unistd.h>
#endif /* HAS_UNISTD */
#include <errno.h>
#include <limits.h>
#include "compat.h"
Expand Down Expand Up @@ -5155,16 +5152,12 @@ wasmCWriteModule(
WasmFunctionIDs staticFunctionIDs,
WasmFunctionIDs dynamicFunctionIDs
) {
char outputDir[PATH_MAX];
char outputName[PATH_MAX];
char headerName[PATH_MAX];

const char* outputPath = options.outputPath;

strcpy(outputDir, outputPath);
strcpy(outputName, outputPath);

strcpy(outputDir, dirname(outputDir));
strcpy(outputName, basename(outputName));

strcpy(headerName, outputName);
Expand All @@ -5177,12 +5170,6 @@ wasmCWriteModule(
strcpy(headerExt, ".h");
}

/* Change to output directory */
if (chdir(outputDir) < 0) {
fprintf(stderr, "w2c2: failed to change to output directory %s\n", outputDir);
return false;
}

MUST (wasmCWriteModuleHeader(
module,
moduleName,
Expand Down
43 changes: 38 additions & 5 deletions w2c2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "file.h"
#include "reader.h"
#include "c.h"
#include "stringbuilder.h"
#include "compat.h"

#if HAS_PTHREAD
static char* const optString = "t:f:d:r:pgmch";
Expand All @@ -31,6 +31,13 @@ static char* const optString = "f:d:r:pgmch";
#include <glob.h>
#endif /* HAS_GLOB */

#if HAS_UNISTD
#include <unistd.h>
#endif /* HAS_UNISTD */
#if _WIN32
#include <direct.h>
#endif

#if _WIN32
#include <windows.h>
#endif
Expand Down Expand Up @@ -181,6 +188,10 @@ static
void
cleanImplementationFiles(void) {
char* path = NULL;
int pathCharIndex = 0;
bool allDigits = true;
size_t pathLength = 0;

#if HAS_GLOB
glob_t globbuf;
size_t pathIndex = 0;
Expand All @@ -196,7 +207,7 @@ cleanImplementationFiles(void) {
path = globbuf.gl_pathv[pathIndex];
#elif _WIN32
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile("*.c", &findFileData);
const HANDLE hFind = FindFirstFile("*.c", &findFileData);
if (hFind == INVALID_HANDLE_VALUE) {
if (GetLastError() != ERROR_FILE_NOT_FOUND) {
fprintf(stderr, "w2c2: failed to find files to clean\n");
Expand All @@ -208,10 +219,10 @@ cleanImplementationFiles(void) {
#else
#error "Unable to find files"
#endif
int pathCharIndex = 0;
bool allDigits = true;
pathCharIndex = 0;
allDigits = true;

size_t pathLength = strlen(path);
pathLength = strlen(path);
if (pathLength != W2C2_IMPL_FILENAME_LENGTH) {
continue;
}
Expand Down Expand Up @@ -248,6 +259,24 @@ cleanImplementationFiles(void) {
#endif
}

static
bool
WARN_UNUSED_RESULT
changeToOutputDirectory(
const char* outputPath
) {
char outputDir[PATH_MAX];
strcpy(outputDir, outputPath);
strcpy(outputDir, dirname(outputDir));

if (chdir(outputDir) < 0) {
fprintf(stderr, "w2c2: failed to change to output directory %s\n", outputDir);
return false;
}

return true;
}

int
main(
int argc,
Expand Down Expand Up @@ -415,6 +444,10 @@ main(

getPathModuleName(moduleName, modulePath);

if (!changeToOutputDirectory(outputPath)) {
return 1;
}

if (clean) {
cleanImplementationFiles();
}
Expand Down

0 comments on commit 8b55ab6

Please sign in to comment.