Created
November 30, 2019 10:11
-
-
Save tilkinsc/7b9cbb90a7e29c2eef794e4aeb59543b to your computer and use it in GitHub Desktop.
How to survive without makefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal | |
REM Default env vars | |
IF NOT DEFINED debug set debug=0 | |
IF NOT DEFINED debug_coverage set debug_coverage=0 | |
IF NOT DEFINED GCC set GCC=gcc | |
IF NOT DEFINED AR set AR=ar | |
IF NOT DEFINED MAKE set MAKE=make | |
IF NOT DEFINED OBJCOPY set OBJCOPY=objcopy | |
IF NOT DEFINED GCC_VER set GCC_VER=gnu99 | |
IF NOT DEFINED GDB set GDB=gdb | |
IF NOT DEFINED WARN set WARN=-Wall | |
IF NOT DEFINED OPT set OPT=-O2 | |
IF NOT DEFINED srcdir set srcdir=src | |
IF NOT DEFINED incdir set incdir=include | |
IF NOT DEFINED objdir set objdir=obj | |
IF NOT DEFINED bindir set bindir=bin | |
IF NOT DEFINED libdir set libdir=lib | |
IF NOT DEFINED dlldir set dlldir=dll | |
IF NOT DEFINED resdir set resdir=res | |
IF NOT DEFINED rootdir set rootdir=root | |
IF NOT DEFINED exename set exename=test.exe | |
IF NOT DEFINED libs set libs=-lgdi32 -lopengl32 -lglew32.dll -lglfw3.dll | |
IF NOT DEFINED dlls set dlls=%dlldir%/OpenAL32.dll | |
IF [%debug%] == [0] ( | |
echo Release build. | |
set attrib=-std=%GCC_VER% %WARN% %OPT% | |
set root=%bindir%\Release | |
) ELSE ( | |
echo Debug build. | |
set attrib=-std=%GCC_VER% %WARN% -g -O0 | |
set root=%bindir%\Debug | |
IF [%debug_coverage%] == [1] set attrib=%attrib% -coverage | |
) | |
set dirs=-L%srcdir% -L%libdir% -L%dlldir% -I%srcdir% -I%incdir% | |
REM -------------------------------------------------------------------- | |
IF [] == [%1] ( | |
echo No arguments specified. | |
echo. | |
call :help 0 | |
exit /b 0 | |
) | |
IF [/?] == [%1] ( | |
call :help 0 | |
exit /b 0 | |
) | |
IF [-?] == [%1] ( | |
call :help 0 | |
exit /b 0 | |
) | |
IF [--help] == [%1] ( | |
call :help 0 | |
exit /b 0 | |
) | |
IF [help] == [%1] ( | |
call :help 0 | |
exit /b 0 | |
) | |
IF [spawn] == [%1] ( | |
echo Ensuring workspace folders... | |
mkdir %srcdir% | |
mkdir %incdir% | |
mkdir %objdir% | |
mkdir %bindir% | |
mkdir %libdir% | |
mkdir %dlldir% | |
mkdir %resdir% | |
mkdir %rootdir% | |
echo Done. | |
exit /b 0 | |
) | |
REM -------------------------------------------------------------------- | |
IF [clean] == [%1] ( | |
echo Cleaning build directory... | |
del %objdir%\*.o 2> nul | |
rmdir /S /Q %bindir%\Release 2> nul | |
rmdir /S /Q %bindir%\Debug 2> nul | |
echo Done. | |
exit /b 0 | |
) | |
IF [install] == [%1] ( | |
echo Installing to directory `%2`... | |
IF [] == [%2] ( | |
echo Invalid directory supplied. | |
call :failure | |
exit /b 1 | |
) | |
IF NOT EXIST "%2" ( | |
echo Please create the destination folder first. | |
call :failure | |
exit /b 1 | |
) | |
xcopy /E /Y %root%\* %2 | |
echo Done. | |
exit /b 0 | |
) | |
IF [%1] == [test] ( | |
IF NOT EXIST "%root%\%exename%" ( | |
echo %exename% not found! | |
exit /b 1 | |
) | |
shift /1 | |
pushd %root% | |
echo [%time%] Running %root%\%exename% ... | |
%exename% | |
echo [%time%] Returned with `%ERRORLEVEL%` | |
popd %root% | |
exit /b 0 | |
) | |
IF [%1] == [debug] ( | |
IF NOT EXIST "%root%\%exename%" ( | |
echo %exename% not found! | |
exit /b 1 | |
) | |
IF [%debug%] == [0] ( | |
echo Warning: debug is 0. May of not compiled right. Continue? [Y/N] | |
set /p cont= | |
IF [%cont%] == [n] exit /b 1 | |
IF [%cont%] == [N] exit /b 1 | |
echo Continuing... | |
) | |
shift /1 | |
pushd %root% | |
echo [%time%] Running %GDB% %root%\%exename% ... | |
%GDB% %exename% | |
echo [%time%] Returned with `%ERRORLEVEL%` | |
popd %root | |
exit /b 0 | |
) | |
REM -------------------------------------------------------------------- | |
IF [%1] == [all] ( | |
call :build_all | |
IF [%ERRLVL%] == [0] call :install_all | |
IF NOT EXIST "%root%\%exename%" call :install_all | |
echo Finished. | |
exit /b 0 | |
) | |
REM -------------------------------------------------------------------- | |
echo No arguments have been supplied. | |
echo. | |
call :help 1 | |
exit /b 1 | |
REM -------------------------------------------------------------------- | |
:build_all | |
setlocal EnableDelayedExpansion | |
echo Building... | |
set todo= | |
set src= | |
set obj= | |
FOR /F "tokens=*" %%i IN ('mtime.exe %srcdir%/*.c') DO set src=%%i | |
FOR /F "tokens=*" %%i IN ('mtime.exe %objdir%/*') DO set obj=%%i | |
IF ["%obj%"] == [""] ( | |
set todo=%srcdir%\*.c | |
goto build_all_skip | |
) | |
set _a0=0 | |
set _a1=0 | |
set _a2=0 | |
set _aiter=1 | |
FOR %%a IN (%src%) DO ( | |
IF [!_aiter!] == [1] ( | |
set _a0=%%a | |
set _a1=%%~na | |
) | |
IF [!_aiter!] == [2] ( | |
set _a2=%%a | |
set _aiter=0 | |
set _b0=0 | |
set _b1=0 | |
set _b2=0 | |
set _biter=1 | |
FOR %%b IN (%obj%) DO ( | |
IF [!_biter!] == [1] ( | |
set _b0=%%b | |
set _b1=%%~nb | |
) | |
IF [!_biter!] == [2] ( | |
set _b2=%%b | |
set _biter=0 | |
IF [!_a1!] == [!_b1!] ( | |
IF [!_a2!] GTR [!_b2!] ( | |
echo !_a0! out of sync with !_b0! | |
set todo=!todo! !_a0! | |
) | |
) | |
) | |
set /a _biter=!_biter! + 1 | |
) | |
) | |
set /a _aiter=!_aiter! + 1 | |
) | |
:build_all_skip | |
IF ["%todo%"] == [""] ( | |
echo Nothing to be done. | |
set ERRLVL=1 | |
goto :EOF | |
) | |
echo Compiling... | |
%GCC% %attrib% %dirs% -D__USE_MINGW_ANSI_STDIO=1 -c %todo% | |
echo Migrating object files... | |
xcopy /Y *.o %objdir% | |
del *.o | |
echo Linking... | |
%GCC% %attrib% %dirs% -o %exename% %objdir%\*.o %libs% %dlls% | |
echo Migrating executable... | |
xcopy /Y %exename% %objdir% | |
del %exename% | |
set ERRLVL=0 | |
echo Finished building. | |
goto :EOF | |
endlocal | |
:install_all | |
setlocal | |
IF EXIST "%root%" rmdir /S /Q %root% | |
mkdir %root% %root%\%resdir% | |
echo Installing assets to %root%... | |
xcopy /Y %objdir%\%exename% %root% | |
xcopy /E /Y %dlldir%\* %root% | |
xcopy /E /Y %resdir%\* %root%\res | |
xcopy /E /Y %rootdir%\* %root% | |
echo Finished installing. | |
goto :EOF | |
endlocal | |
endlocal | |
REM Simplex help message | |
:help | |
setlocal | |
echo Usage: | |
echo build.bat all Builds all solutions | |
echo build.bat test Tests the configured solution | |
echo build.bat debug Tests the configured solution with %GDB% | |
echo build.bat clean Cleans the environment of built files | |
echo build.bat install [directory] Installs to a pre-created directory | |
echo build.bat spawn Ensures required directories are present | |
echo build.bat -? /? --help Shows this help message | |
echo. | |
echo Configured as: | |
echo debug=%debug% | |
echo debug_coverage=%debug_coverage% | |
echo GCC=%GCC% | |
echo AR=%AR% | |
echo MAKE=%MAKE% | |
echo OBJCOPY=%OBJCOPY% | |
echo GCC_VER=%GCC_VER% | |
echo GDB=%GDB% | |
echo WARN=%WARN% | |
echo OPT=%OPT% | |
echo. | |
echo Runtime: | |
echo root=%root% | |
echo attrib=%attrib% | |
echo srcdir=%srcdir% | |
echo incdir=%incdir% | |
echo objdir=%objdir% | |
echo bindir=%bindir% | |
echo libdir=%libdir% | |
echo dlldir=%dlldir% | |
echo resdir=%resdir% | |
echo rootdir=%rootdir% | |
echo dirs=%dirs% | |
echo. | |
goto :EOF | |
endlocal | |
REM General failure message | |
:failure | |
setlocal | |
echo An error has occured! Display help? | |
set /p temp= | |
IF [%temp%] == [y] call :help 1 | |
IF [%temp%] == [Y] call :help 1 | |
goto :EOF | |
endlocal | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment