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

Improve glue code generation #729

Merged
merged 1 commit into from
Mar 31, 2020
Merged

Conversation

Jj0YzL5nvJ
Copy link
Contributor

These changes make us independent of gawk in Windows, and avoid issues with AppVeyor listed in #722

More info in #728

@Jj0YzL5nvJ Jj0YzL5nvJ mentioned this pull request Mar 1, 2020
@Gillou68310
Copy link
Member

I like the idea of using a script for generating asm defines but batch really is an awfull scripting language IMO. Do you think we can use something that could be cross platform?

@Jj0YzL5nvJ
Copy link
Contributor Author

Jj0YzL5nvJ commented Mar 1, 2020

Hmm... the Travis CI indicator has glitched. But it passed.

Do you think we can use something that could be cross platform?

Python, Lua, Perl, Ruby, JavaScript. Any language works, the task is stupidly easy.

Display an object as a list, look for the @ASM_DEFINE pattern, take element $2 and $3 on that line, print #define "$2" ("$3") to asm_defines_gas.h and print %define "$2" ("$3") to asm_defines_nasm.h, that's all.

but batch really is an awfull scripting language IMO

It's funny, we're literally using batch for AppVeyor building, not to mention that Visual Studio uses native functions for commands (by default CMD). Batch Inception.
P.S: Most of the code in my batch is an anchor ensuring the position of key elements.

@Gillou68310
Copy link
Member

It's funny, we're literally using batch for AppVeyor building, not to mention that Visual Studio uses native functions for commands (by default CMD). Batch Inception.
P.S: Most of the code in my batch is an anchor ensuring the position of key elements.

I do not have control over those ;-)
If we can avoid having a batch script in core repo I would be a happy man.

Python, Lua, Perl, Ruby, JavaScript. Any language works, the task is stupidly easy.

I do like python, I know it's an easy task that's why I think the batch script looks complicated for the task.

@Jj0YzL5nvJ
Copy link
Contributor Author

Jj0YzL5nvJ commented Mar 1, 2020

I prefer anything within VS commands that return a logical error code when they fail, that a mixture command string that returns random values. Or always 0 (like gawk in this case) o_O.

I do like python, I know it's an easy task that's why I think the batch script looks complicated for the task.

You're welcome, BTW... don't forget to use cp1252 if you detect a Windows OS, utf-8 is incorrect. But in this particular case, probably utf-8 is safe...

@Mastergatto
Copy link
Contributor

I'm not sure if this is the case, probably I misunderstood this whole thing, but some time ago @bsmiles32 made an effort to migrate the code to meson, python-based build system (http://mesonbuild.com/)
#488

@Jj0YzL5nvJ
Copy link
Contributor Author

Jj0YzL5nvJ commented Mar 1, 2020

I think the batch script looks complicated for the task.

This is the code without security measures:

for /f "tokens=1,2*" %%A in ('echo %*') do set GAS_VS_ARG1=%%A& set GAS_VS_ARG2=%%B& set GAS_VS_ARG3TOINF=%%C
del %GAS_VS_ARG2%asm_defines_*
cl /c /Fo%GAS_VS_ARG1% %GAS_VS_ARG3TOINF% /I ..\..\src %GAS_VS_ARG2%asm_defines.c
for /f "tokens=2,3" %%J in ('type %GAS_VS_ARG1%asm_defines.obj ^| find "@ASM_DEFINE"') do (
echo #define %%J ^(%%K^)>>%GAS_VS_ARG2%asm_defines_gas.h
echo %%define %%J ^(%%K^)>>%GAS_VS_ARG2%asm_defines_nasm.h
)

The proposed code contains the minimum security conditions "for monkeys on the keyboard".

@Mastergatto, Meson is another completely different beast.

@Narann
Copy link
Member

Narann commented Mar 2, 2020

That's a big job, but wow, it's not nice to read.

@Jj0YzL5nvJ
Copy link
Contributor Author

Jj0YzL5nvJ commented Mar 2, 2020

I take that as a compliment, even knowing that it's not.

The part that requires a clear explanation even knowing the language is this:

set GAS_RPATH_COUNT=1
:ADD
set GAS_RPATH=
for /f "tokens=%GAS_RPATH_COUNT% delims=\" %%a in ('cd') do set GAS_RPATH=%%a
if "%GAS_RPATH%" NEQ "" set /a GAS_RPATH_COUNT=%GAS_RPATH_COUNT%+1& goto ADD
set /a GAS_RPATH_COUNT=%GAS_RPATH_COUNT%-2
if %GAS_RPATH_COUNT% LEQ 2 exit /b 22

Many programs in Windows inherit the open path of the program that invokes them, in this case VS. If VS launch the script from the shortest possible logical path x:\mupen64plus-core\projects\msvc\, then at the end of the directory count, we subtract the 2 unwanted, drive letter and the last count to destroy the GAS_RPATH, and the result can never be less than or equal to 2 (R1). With this test it's guaranteed that an erroneous relative route is not possible due to being unable to go backwards more for the current GAS_VS_ARG2 (..\..\src\asm_defines\) later.

Edit:
P.S: The new logic makes more sense.
P.S 2: Syntax conflict...

@Gillou68310
Copy link
Member

Ok let's not add python dependency to core building. Even if batch is awfull to read, it does the job without external dependencies so that's a pretty strong point.

@Gillou68310
Copy link
Member

If this is merged we can get rid of gawk in win32-deps!

@Jj0YzL5nvJ Jj0YzL5nvJ force-pushed the improve branch 2 times, most recently from f236fe1 to 4caa7dd Compare March 2, 2020 22:49
@Jj0YzL5nvJ
Copy link
Contributor Author

Jj0YzL5nvJ commented Mar 2, 2020

Well, I made the code a little more descriptive, I have the bad habit of wanting to make the code take up too little bytes... floppy mindset.
I hadn't noticed it, I barely noticed it when I woke up ^^u

Even if batch is awfull to read

And even more my batch =P

Edit: OMG, bilingual dyslexia... that new in myself.

@Narann
Copy link
Member

Narann commented Mar 3, 2020

TBH, I think one comment per line, starting from the for loop, wouldn't be too much.

@Jj0YzL5nvJ Jj0YzL5nvJ force-pushed the improve branch 3 times, most recently from 9009123 to f57fb96 Compare March 4, 2020 08:03
@Jj0YzL5nvJ
Copy link
Contributor Author

Jj0YzL5nvJ commented Mar 4, 2020

More syntax, more variables to mess up, new logic, more intricated code, more robust, more mindfuck, BETTER.
After this, I need to look for latent errors in my old scripts and add new optimizations.
/人◕ ‿‿ ◕人\

The only way to understand the code is to play with it. This is already in "debug" mode, run this from CMD, change GAS_VS_ARG2 to whatever you want and move the script to different folder levels to see the differences, a USB is fine too. To see error codes, execute echo% errorlevel%.
Using pause after each command also works.

@echo on
setlocal enableextensions disabledelayedexpansion
:: Forced destruction of environment variables, these can also be modified to debug errors
set GAS_VS_ARG1=qwer\zxcv\
set GAS_VS_ARG2=..\..\Hi
set GAS_VS_ARG3TOINF=asdf
:: Adaptation of the parameters sent by VS to use them as variables, if an invalid parameter is sent (syntax conflict), this script will be closed and will not generate an error code for ERRORLEVEL
REM for /f "tokens=1,2*" %%A in ('echo %*') do set GAS_VS_ARG1=%%A& set GAS_VS_ARG2=%%B& set GAS_VS_ARG3TOINF=%%C
:: Making sure we are in the tools folder, it's not really necessary...
REM echo "%~dp0" | find /i "\mupen64plus-core\tools\" >nul
REM if errorlevel 1 exit /b 11
:: Initialized as 1 to execute "tokens", it will be subtracted later
set GAS_RPATH_COUNT=1
:: Initialized with the ABSOLUTE PATH of the VS project file as FIRST condition
set GAS_RPATH_CONDITION=%CD%
set GAS_RPATH_COUNT_SHADOW=0
set GAS_RPATH_MINUS_SHADOW=0
:ADD
:: Destroying GAS_RPATH in each loop because if it can't be reinitialized in 'for', it will return the last valid value
set GAS_RPATH=
for /f "tokens=%GAS_RPATH_COUNT% delims=\" %%a in ('echo %GAS_RPATH_CONDITION%') do set GAS_RPATH=%%a
:: The value '..' can ONLY come from GAS_RPATH_CONDITION = GAS_VS_ARG2, a RELATIVE PATH
if "%GAS_RPATH%"==".." set /a GAS_RPATH_MINUS_SHADOW=%GAS_RPATH_MINUS_SHADOW%+1
:: Addition to GAS_RPATH_COUNT if GAS_RPATH is defined
if defined GAS_RPATH set /a GAS_RPATH_COUNT=%GAS_RPATH_COUNT%+1& goto ADD
:: Correcting GAS_RPATH_COUNT, 1 to execute "tokens" and another one of the last destruction of GAS_RPATH
set /a GAS_RPATH_COUNT=%GAS_RPATH_COUNT%-2
:: Single use loop... to set GAS_RPATH_COUNT_SHADOW, GAS_RPATH_MINUS_SHADOW and the GAS_RPATH_CONDITION's SECOND condition as RELATIVE PATH with GAS_VS_ARG2
if "%GAS_RPATH_CONDITION%" NEQ "%GAS_VS_ARG2%" set GAS_RPATH_COUNT_SHADOW=%GAS_RPATH_COUNT%& set GAS_RPATH_CONDITION=%GAS_VS_ARG2%& set GAS_RPATH_COUNT=1& goto ADD
:: Theoretical error... artificial too, this script ONLY counts folders
if %GAS_RPATH_COUNT_SHADOW% LEQ 0 exit /b 9999
set /a GAS_RPATH_COUNT=%GAS_RPATH_COUNT_SHADOW%-%GAS_RPATH_MINUS_SHADOW%
:: Logical check for ABSOLUTE PATH vs RELATIVE PATH, isn't a truly valid check
if %GAS_RPATH_COUNT% LEQ -1 exit /b 22
pause

@Jj0YzL5nvJ
Copy link
Contributor Author

Yeah... too complex, good for me, bad for everything else. Take the Lite version.

@Jj0YzL5nvJ Jj0YzL5nvJ changed the title Improve quality at building glue code Improve glue code generation Mar 4, 2020
@Narann
Copy link
Member

Narann commented Mar 30, 2020

Does it make sense to add some comments in the current gen_asm_script.cmd, at leat to globally understand what it does?

If we can't add comments, I will merge it like this but a general explanation of the script would be great.

@Narann
Copy link
Member

Narann commented Mar 31, 2020

Thanks a lot @Jj0YzL5nvJ, it's perfect !

@Narann Narann merged commit de4d743 into mupen64plus:master Mar 31, 2020
@Jj0YzL5nvJ Jj0YzL5nvJ deleted the improve branch April 1, 2020 04:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants