Skip to content

Commit 301d4da

Browse files
committed
Potential solution for #111
1 parent 8ddbc72 commit 301d4da

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* eol=lf

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
nob
22
nob.old
33
nob.exe
4-
build/
4+
build/
5+
**/*.obj

nob.h

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@
9999
#ifndef NOB_H_
100100
#define NOB_H_
101101
#ifdef _WIN32
102-
#define _CRT_SECURE_NO_WARNINGS (1)
102+
# ifndef _CRT_SECURE_NO_WARNINGS
103+
# define _CRT_SECURE_NO_WARNINGS (1)
104+
# endif
103105
#endif
104106

105107
#ifndef NOBDEF
@@ -840,6 +842,26 @@ NOBDEF void nob__go_rebuild_urself(int argc, char **argv, const char *source_pat
840842
if (rebuild_is_needed < 0) exit(1); // error
841843
if (!rebuild_is_needed) { // no rebuild is needed
842844
NOB_FREE(source_paths.items);
845+
846+
#ifdef NOB_EXPERIMENTAL_DELETE_OLD
847+
# ifdef _WIN32
848+
if (argc <= 0) return;
849+
850+
// Check if our parent nob wants to be deleted
851+
char *argument = nob_shift(argv, argc);
852+
if (strcmp("--nob-delete-old", argument) == 0) {
853+
// Wait a bit so the old nob exits
854+
// Better would be to WaitForSingleObject on the handle of the old nob, but we cannot pass its handle?
855+
Sleep(10);
856+
argument = nob_shift(argv, argc);
857+
nob_delete_file(argument);
858+
} else {
859+
// Restore the arguments
860+
argc++; argv--;
861+
}
862+
# endif
863+
#endif // NOB_EXPERIMENTAL_DELETE_OLD
864+
843865
return;
844866
}
845867

@@ -855,13 +877,26 @@ NOBDEF void nob__go_rebuild_urself(int argc, char **argv, const char *source_pat
855877
exit(1);
856878
}
857879
#ifdef NOB_EXPERIMENTAL_DELETE_OLD
880+
# ifndef _WIN32
858881
// TODO: this is an experimental behavior behind a compilation flag.
859882
// Once it is confirmed that it does not cause much problems on both POSIX and Windows
860883
// we may turn it on by default.
861884
nob_delete_file(old_binary_path);
885+
# endif
862886
#endif // NOB_EXPERIMENTAL_DELETE_OLD
863887

864888
nob_cmd_append(&cmd, binary_path);
889+
890+
#ifdef NOB_EXPERIMENTAL_DELETE_OLD
891+
# ifdef _WIN32
892+
// On Windows, tell the new nob to delete us, always pass as the second argument
893+
// so we can consume it before any user defined arguments.
894+
nob_cmd_append(&cmd, "--nob-delete-old", old_binary_path);
895+
Nob_Procs procs = {0}; // Small leak until we exit the current nob
896+
opt.async = &procs;
897+
# endif
898+
#endif // NOB_EXPERIMENTAL_DELETE_OLD
899+
865900
nob_da_append_many(&cmd, argv, argc);
866901
if (!nob_cmd_run_opt(&cmd, opt)) exit(1);
867902
exit(0);
@@ -1860,7 +1895,7 @@ NOBDEF bool nob_rename(const char *old_path, const char *new_path)
18601895
{
18611896
nob_log(NOB_INFO, "renaming %s -> %s", old_path, new_path);
18621897
#ifdef _WIN32
1863-
if (!MoveFileEx(old_path, new_path, MOVEFILE_REPLACE_EXISTING)) {
1898+
if (!MoveFileEx(old_path, new_path, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {
18641899
nob_log(NOB_ERROR, "could not rename %s to %s: %s", old_path, new_path, nob_win32_error_message(GetLastError()));
18651900
return false;
18661901
}

0 commit comments

Comments
 (0)