Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to output directory before cleaning #87

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
51 changes: 42 additions & 9 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,10 +444,6 @@ main(

getPathModuleName(moduleName, modulePath);

if (clean) {
cleanImplementationFiles();
}

{
WasmModuleReader reader = emptyWasmModuleReader;
WasmCWriteModuleOptions writeOptions = emptyWasmCWriteModuleOptions;
Expand Down Expand Up @@ -474,6 +499,14 @@ main(
functionsPerFile = UINT32_MAX;
}

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

if (clean) {
cleanImplementationFiles();
}

writeOptions.outputPath = outputPath;
writeOptions.threadCount = threadCount;
writeOptions.functionsPerFile = functionsPerFile;
Expand Down