File indexing completed on 2024-05-26 09:34:39

0001 #!/bin/awk
0002 #
0003 # Copyright (c) 2016, Michal Policht. This file is dually licensed under terms of 
0004 # either WTFPL or BEER-WARE LICENSE. You may obtain the copy of WTFPL or BEER-WARE
0005 # LICENSE by googling it.
0006 #
0007 # THIS SOFTWARE IS ADDICTED TO DRUGS AND ALCOHOL AND IS A SUSPECT OF DRIVING UNDER THE 
0008 # INFLUENCE OF ALCOHOL. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
0009 # CONTRIBUTORS "AS IS NOT" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
0010 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS AND APPLICATION AND 
0011 # ACCOMODATION FOR A PARTICULAR PURPOSE AND MANY OTHERS NOT MENTIONED, NOR EVEN
0012 # INVENTED WARRANTIES ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 
0013 # OR CONTRIBUTORS BE LIABLE FOR ANY TEMPORAL, PERMANENT, UNFORTUNATE, UNLUCKY,
0014 # LUCKY, DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, ACCIDENTAL, DENTAL, MENTAL, 
0015 # ELEMENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
0016 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION; 
0017 # OR BEING KILLED BY A LIGHTNING; OR FLOOD; OR FIRE; OR ANTEATER (INCLUDING BUT NOT 
0018 # LIMITED TO, GIANT ANTEATER, ANTEATER WHO LIES ON A SOFA; ANTEATER WHO LISTENS HEAVY 
0019 # METAL MUSIC; ANTEATER WHO ATE HUMAN BEFORE) OR ANOTHER BEING.) HOWEVER CAUSED AND ON 
0020 # ANY, ANY THEORY OF LIABILITY, CONSPIRACY THEORY, ANY THEORY OF RELATIVITY, WHETHER IN 
0021 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 
0022 # WAY OUT OF THE USE OF THIS SOFTWARE, ARISING IN ANY WAY OUT OF THE SUN, EVEN IF PROMISSED 
0023 # WISHED OR ASSURED SUCH DAMAGE. THIS SOFTWARE CAN DO TERRIBLE THINGS.
0024 #
0025 # Script to collect sources and put them into CMakeLists.txt file. It substitutes section 
0026 # between magic strings with a generated list of sources.
0027 # Magic strings are: ^#sources_begin, ^#sources_end (^ denotes beginning of the line).
0028 #
0029 # usage: awk [-v src_dirs="dir1 dir2 ..."] [-v src_files="pattern1;pattern2;..."] [-v cmake_var="VAR"] -f cmakesrcs.awk CMakeList.txt
0030 # example: awk -v src_dirs="src/dir1 src/dir2" -v src_files="*.cxx;*.c" -v cmake_var="SOURCES" -f cmakesrcs.awk CMakeList.txt
0031 #
0032 # parameters: 
0033 # find - find program.
0034 # src_dirs - where to look for sources (default: "*").
0035 # cmake_var - cmake sources variable name (default: "SRCS").
0036 # src_files - list of source file patterns separated by a semicolon (default: "*.cpp;*.c").
0037 #
0038 # required tools: awk, find (findutils).
0039 #
0040 # Note on MS Windows: find shipped with findutils may be shadowed by Windows' find.exe,  
0041 # residing in Windows/system32 folder. Windows find.exe is a different tool and it has to
0042 # be moved behind findutils find in script search path. Alternatively you can pass find 
0043 # parameter to the script pointing to findutils find binary.
0044 
0045 
0046 BEGIN {
0047     if (!find)
0048         find = "find"
0049   if (!src_dirs)
0050     src_dirs = "*";
0051   if (!cmake_var)
0052     cmake_var = "SRCS";
0053   if (!src_files)
0054     src_files = "*.cpp;*.c";
0055   split(src_files, src_files_arr, ";");
0056 
0057   sources_section = 0;
0058 }
0059 
0060 /^#sources_begin/ {
0061   sources_section = 1; 
0062     ors_str = ORS
0063     gsub(/\n/, "\\\\n", ors_str)
0064     gsub(/\r/, "\\\\r", ors_str)
0065   print $0;
0066   print "SET(" cmake_var;
0067   for (src_file_index in src_files_arr)
0068     system(find " " src_dirs " -name '" src_files_arr[src_file_index] "' -printf %p" ors_str);
0069   print ")";
0070 }
0071 
0072 {
0073   if (!sources_section)
0074     print $0;
0075 }
0076 
0077 /^#sources_end/ {
0078   sources_section = 0; 
0079   print $0 
0080 }