summaryrefslogtreecommitdiff
path: root/usr.bin/make/dir.c
AgeCommit message (Collapse)Author
2023-06-23make: warn about malformed patterns in ':M', ':N' and '.if make(...)'rillig
These patterns shouldn't occur in practice, as their results are tricky to predict. Generate a warning for now, and maybe an error later. Reviewed by sjg@.
2023-06-22make: clean up comments related to pattern matchingrillig
2023-01-24make: .SYSPATH: to add dirs to sysIncPathsjg
.SYSPATH: with no sources will clear sysIncPath otherwise sources are added Reviewed by: rillig
2022-05-07make: fix grammar in comment of DirFindDotrillig
2022-02-04make: use unsigned int for line numbers everywhererillig
Previously, some line numbers were stored as signed int while others were stored as size_t. Since line numbers are never negative, use an unsigned type. Since the maximum file size for makefiles is 1 GB (see loadfile), unsigned int is large enough even on 64-bit platforms. Using a single data types reduces the number of type conversions. Using unsigned int improves compatibility with C90 (printf %u instead of %zu), which is needed by bmake, which is derived from usr.bin/make. No functional change.
2022-01-30Make the GNode lineno unsigned to fix lint warning in var.c callingchristos
PrintLocation()
2021-12-15make: use consistent indentation for statements and continuationsrillig
No binary change, except for line numbers in assertions in suff.c.
2021-11-28make: eliminate CachedStatsFlagsrillig
Having two boolean flags as parameters should be easier to understand than bit manipulations. The variable names now match more directly. No functional change.
2021-11-28make: convert GNodeFlags from enum into bit-fieldsrillig
Now that Enum_ToString is implemented for each type separately, it's easy to convert them to bit-fields. This gets rid of the magic numbers 12 for CYCLE and 13 for DONECYCLE that left a suspicious gap in the numbers. This gap was not needed since the code didn't make use of the relative ordering of the enum constants. The effects of this conversion are fewer capital letters in the code, smaller scope for the GNode flags, and clearer code especially when setting a flag back to false. One strange thing is that GCC 10.3.0 doesn't optimize GNodeFlags_IsNone to an single bitmasking instruction, at least on x86_64. Instead it generates a testb instruction for each of the flags, even loading bit 8 separately from the others. Clang 12.0.1 knows this optimization though and generates the obvious sequence of movzwl, testl, jz. No functional change.
2021-09-21make: remove unnecessary const from parametersrillig
These were leftovers from earlier refactorings, when extracting code to separate functions. No functional change.
2021-04-04make: remove filler word 'Do' from function names for parsingrillig
No functional change, except for debug logging.
2021-04-03make: use C99 bool type instead of defining its ownrillig
No functional change.
2021-02-05make: add const to SearchPath_Printrillig
2021-02-05make: add shortcut Global_Delete for deleting a global variablerillig
2021-02-04make: rename some VAR constants to SCOPErillig
The word "context" does not fit perfectly to the variables that are associate with a GNode, as the context is usually something from the outside and the variables are more like properties inherent to the GNode. The term "global context" fits even less. Since the thing where variables are looked up is commonly named a scope, use that term instead. This commit only renames the global variables VAR_GLOBAL, VAR_INTERNAL and VAR_CMDLINE, plus a few very closely related comments. These are: GNode.vars (because of line breaks) GNode_Free (dito) varname-make_print_var_on_error.mk varname-make_print_var_on_error-jobs.mk The debug message in Var_Stats is left as-is since there is no unit test for it yet. The other renamings (variable names "context", "ctxt", as well as further comments) will be done in a follow-up commit.
2021-02-03make: replace Global_AppendExpand with Global_Appendrillig
All callers with a variable name that is guaranteed to not contain a dollar sign have been converted to call Global_Append instead of the previous Global_AppendExpand. After that, Global_AppendExpand was unused, therefore it was effectively just renamed.
2021-02-03make: use shortcut functions Global_SetExpand and Global_AppendExpandrillig
There are many places where global variables are set or appended to. To reduce clutter and code size, encode the VAR_GLOBAL in the function name. The word Expand in the function names says that the variable name is expanded. In most of the cases, this is not necessary, but there are no corresponding functions Global_Set or Global_Append yet. Encoding the information whether the name is expanded or not in the function name will make inconsistencies obvious in future manual code reviews. Letting the compiler check this by using different types for unexpanded and expanded variable names is probably not worth the effort. There are still a few bugs to be fixed, such as in SetVar, which expands the variable name twice in a row.
2021-01-30make(1): split Buf_Destroy into Buf_Done and Buf_DoneDatarillig
In all cases except one, the boolean argument to Buf_Destroy was constant. Removing that argument by splitting the function into two separate functions makes the intention clearer on the call site. It also removes the possibility for using the return value of Buf_Done, which would have made no sense. The function Buf_Done now pairs with Buf_Init, just as in HashTable and Lst. Even though Buf_Done is essentially a no-op, it is kept as a function, both for symmetry with Buf_Init and for clearing the Buffer members after use (this will be done only in CLEANUP mode, in a follow-up commit).
2021-01-24make(1): convert SearchPath to structrillig
This prepares for making dotLast a simple struct member instead of a fake CachedDir, which is easier to understand.
2021-01-23make(1): split Dir_FindFile into separate functionsrillig
2021-01-23make(1): remove the remaining beasts from the commentsrillig
2021-01-23make(1): extend comments in dir.crillig
2021-01-23make(1): rename Dir_AddDir, reorder parameters of SearchPath_ToFlagsrillig
2021-01-23make(1): extract SearchPath_ExpandMiddle from SearchPath_Expandrillig
2021-01-23make(1): split local variable in SearchPath_Expandrillig
2021-01-23make(1): rename parameter of SearchPath_Expandrillig
2021-01-23make(1): rename Dir_Expand to SearchPath_Expandrillig
The main subject of this function is the search path. In this search path the pattern is expanded.
2021-01-10make(1): consistently use boolean expressions in conditionsrillig
Most of the make code already followed the style of explicitly writing (ptr != NULL) instead of the shorter (ptr) in conditions. The remaining 50 instances have been found by an experimental, unpublished check in lint(1) that treats bool expressions as incompatible to any other scalar type, just as in Java, C#, Pascal and several other languages. The only unsafe operation on Boolean that is left over is (flags & FLAG), for an enum implementing a bit set. If Boolean is an ordinary integer type (the default), some high bits may get lost. But if Boolean is the same as _Bool (by compiling with -DUSE_C99_BOOLEAN), C99 6.3.1.2 defines that a conversion from any scalar to the type _Bool acts as a comparison to 0, which cannot lose any bits.
2020-12-30make(1): format multi-line commentsrillig
2020-12-27make(1): exit 2 on technical errorsrillig
This allows the -q option to distinguish errors from out-of-date targets. Granted, it's an edge case but it should be solved consistently anyway. The majority of cases in which make exits with exit status 1, even in -q mode, is when there are parse errors. These have been kept as-is for now as they affect many of the unit tests. The technical errors, on the other hand, occur so rarely that it's hard to write reliable tests for them that fail consistently on all platforms supported by make.
2020-12-13make(1): add str_basename to reduce duplicate coderillig
The function basename from POSIX has a few unfortunate properties, it is allowed to return a pointer to static memory. This is too unreliable, therefore this trivial own implementation.
2020-12-06make(1): remove comment decorationrillig
2020-12-06make(1): inline macros for debug loggingrillig
No changes to the resulting binary, except for the line numbers in assertions.
2020-12-04make(1): use consistent variable names for list nodesrillig
2020-12-04make(1): use fixed format for debug output of the directory cacherillig
The previous output format had a %-20s conversion specifier. This produced different output depending on the length of the pathname, which was too difficult to normalize. By moving the directory name to the end, it is no longer necessary to fill up any space, and the numbers are always aligned properly. As a result, 3 of the unit tests no longer need any special postprocessing of their output.
2020-12-01make(1): rename local variable in Dir_SetPATHrillig
The variable name should reflect the close relationship to the .DOTLAST keyword that can be used in search paths.
2020-12-01make(1): remove Dir_InitDirrillig
The function name had been too ambiguous since it didn't mention the particular directory that was initialized. Instead of that function, Dir_InitCur is called directly from main_Init. The pseudo CachedDir entry ".DOTLAST" is initialized at the very beginning. The observable behavior is unchanged since this a memory-only object with no connection to the file system.
2020-11-30make(1): clean up variable names in Dir_FindFilerillig
The special path entry is called .DOTLAST, therefore the local variable should have the same name. A variable named 'base' must not point to the slash of a pathname. It may only point to the character after the slash, everything else is confusing, even if it's only for a brief moment.
2020-11-30make(1): document difference between 'cur' and 'dot'rillig
2020-11-30make(1): fix memory leak for lstat cache in -DCLEANUP moderillig
2020-11-29make(1): initialize global variables in dir.crillig
Calling CachedDir_Assign requires that the variable be initialized. On most systems, NULL is represented as all-zero bits already. This change is only for the few other systems. Add some comments explaining the implementation of Dir_AddDir since that is tricky to read from the code alone.
2020-11-29make(1): clean up memory management for CachedDirsrillig
Previously, the reference count for a newly created CacheDir had been set to 1 in CacheNewDir. This was wrong because at that point, the object had not been referenced by any nonlocal variable. The reference count is no longer incremented at this point. All callers of CacheNewDir either append the newly created CachedDir to a SearchPath via Lst_Append and CachedDir_Ref, or they assign it to a global variable via CachedDir_Assign. Since the reference count is no longer wrongly incremented, it does not need to be decremented more than necessary in Dir_End. To keep the code simple and maintainable, all assignments to global variables are now handled by CachedDir_Assign. Adding a CachedDir to a list is still done manually via Lst_Append, and the corresponding code for decrementing is in SearchPath_Clean and SearchPath_Free. These details may be cleaned up in a follow-up commit. As a result, when OpenDirs_Done is called in the unit tests, the list of open directories is empty. It had been non-empty in a single unit test before (dep-wildcards.mk), as a result of calling Dir_Expand. The additional debug logging for the reference counting is not enabled by default since it contains memory addresses, which makes the output dependent on the memory allocator. The function CachedDir_Destroy has been merged into CachedDir_Undef, which had only been used in Dir_End before. The new name emphasizes that it corresponds to CachedDir_Ref.
2020-11-29make(1): fix the reference count of dotLast going negativerillig
The memory management for dotLast is quite simple. It is initialized exactly once main_Init > Init_Objdir > Dir_InitDir and freed exactly once in main_CleanUp > Dir_End. Previously, dotLast was not freed at all. The first call to CachedDir_Unref decremented the refCount to 0 but didn't free anything. Next, CachedDir_Destroy was called, which decremented the reference count to -1, therefore skipping the actual freeing. This was probably an implementation mistake. Since Dir_End is called at the very end of main_CleanUp, no code accesses dotLast after it has been freed.
2020-11-29make(1): move CachedDir_Destroy up to the related functionsrillig
2020-11-29make(1): extract CachedDir_Free0 from CachedDir_Destroyrillig
2020-11-29make(1): remove wrong comment in Dir_InitCurrillig
In a makefile with repeated ".CURDIR=." lines, Dir_AddDir is called with a NULL path, once per line. Since the path is NULL, the search for OpenDirs_Find is skipped and the directory is always read from disk. The freshly read directory has a refCount of 1, and the refCount never raises above 2. In Dir_InitCur, the directory of the previous .CURDIR has a refCount of 2, which is decremented twice and then freed. After this, the new directory is placed in the global 'cur', after incrementing its refCount to 2. It still seems wrong that the refCount of 'cur' is 2 instead of 1, but it works well.
2020-11-29make(1): add debug logging for OpenDirs_Donerillig
2020-11-29make(1): extract CacheNewDir from Dir_AddDirrillig
Change the debug output for directories that are not found.
2020-11-29make(1): make documentation of CachedDir.refCount more preciserillig
2020-11-29make(1): add debug logging for reference counting of CachedDirrillig