Compiler Options in source code

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Compiler Options in source code

Post by BarryG »

Hi, can we please set Compiler Options in our sources as well as through Compiler Options? If in the source, it could override whatever Compiler Options are set.

Rationale: I literally just spent THREE HOURS going through my 830 KB (20,000 lines) source trying to find why my program kept crashing with memory errors. Couldn't find a reason, and only now just discovered it was simply because I forgot to turn thread safety on. This was because I pasted my source into a new PureBasic instance earlier today, and totally forgot that pasting the source will not have any Compiler Options in it. Not fun.

Thanks for considering.
Last edited by BarryG on Fri May 10, 2019 7:52 am, edited 2 times in total.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Compiler Options in source code

Post by Marc56us »

+1
But in the meantime, there is a method to avoid this classic mistake

Code: Select all

EnableExplicit

CompilerIf #PB_Compiler_Thread = #False
    MessageRequester("Warning !!",
                     "You must enable ThreadSafe support in compiler options",
                     #PB_MessageRequester_Ok )
    End
CompilerEndIf 

; ...
(source: many codes in this forum)

:wink:
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Compiler Options in source code

Post by Josh »

This will not be possible because some settings will have to be specified when the compiler is started.

Do it as you can see in many examples here in the forum. Ask for the settings at the beginning of your program and if one is not correct, issue an error message. You can do it on a simple way:

Code: Select all

CompilerIf #PB_Compiler_Thread = #False
  "ThreadSave must be activated"
CompilerEndIf
Last edited by Josh on Fri May 10, 2019 6:27 am, edited 1 time in total.
sorry for my bad english
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Re: Compiler Options in source code

Post by BarryG »

Josh wrote:This will not be possible because some settings will have to be specified when the compiler is started.
Ok, I didn't realise. I've put this in instead, at the recommendations above:

Code: Select all

CompilerIf #PB_Compiler_Thread=0
  Debug "Enable thread-safety in Compiler Options!"
  End
CompilerEndIf
Marc56us wrote:classic mistake
It was a nightmare for those three hours! :evil:

Thought: Since the compiler knows that thread-safety is OFF at compile time (via #PB_Compiler_Thread), why can't it throw a message when it first encounters a CreateThread() command when compiling, and alert us of this fact? Good idea? Would've solved my pain and time.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Compiler Options in source code

Post by Josh »

BarryG wrote:
Marc56us wrote:classic mistake
It was a nightmare for those three hours! :evil:

Thought: Since the compiler knows that thread-safety is OFF at compile time (via #PB_Compiler_Thread), why can't it throw a message when it first encounters a CreateThread() command when compiling, and alert us of this fact? Good idea? Would've solved my pain and time.
I'm not an expert on threads but I believe that it is not necessary in all cases that the ThreadSave option must be set. So an automatic error message would be counterproductive.


Three hours troubleshooting? You noob :mrgreen:
Have spent the last two days finding a very nasty mistake
Last edited by Josh on Fri May 10, 2019 6:51 am, edited 1 time in total.
sorry for my bad english
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Re: Compiler Options in source code

Post by BarryG »

Josh wrote:it is not necessary in all cases that the ThreadSave option must be set.
But a warning message would've at least been useful when compiling (at the bottom of the IDE in the status area, maybe). It could be something as simple as: "FYI: Thread safety is not enabled, and you're using threads."
Josh wrote:Three hours troubleshooting? You noob :mrgreen:
Have spent the last two days finding a very nasty mistake
Yuck. I feel your pain.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [Solved] Compiler Options in source code

Post by mk-soft »

Code: Select all

CompilerIf #PB_Compiler_Thread = #False
  CompilerError "ThreadSave must be activated at main file"
CompilerEndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Compiler Options in source code

Post by Little John »

+1
Josh wrote:This will not be possible because some settings will have to be specified when the compiler is started.
It will be possible if those compiler options are at the very beginning of the source code.
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Compiler Options in source code

Post by skywalk »

The settings are in the file as trailing comments. Better if at top in user viewable code.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Compiler Options in source code

Post by Little John »

skywalk wrote:The settings are in the file as trailing comments.
Yes ... if the preferences are set accordingly. And actually that's the proof that storing the settings in the source file does work. :-)
skywalk wrote:Better if at top in user viewable code.
I disagree. The problem currently is, that those settings are not visible in the part of the code shown in the IDE. That means that the visible part of the source -- which is also e.g. shared here on the forum -- is not self-documenting.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Compiler Options in source code

Post by kenmo »

You cannot set a compiler constant in your code, but you CAN prevent compiling if the settings are incorrect.

Quick and dirty way:

Code: Select all

; Build fails if Thread is #False
#PB_Compiler_Thread = #True
Nicer way:

Code: Select all

CompilerIf (Not #PB_Compiler_Thread)
  CompilerError #PB_Compiler_Filename + " requires Threadsafe mode"
CompilerEndIf
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Compiler Options in source code

Post by Josh »

Little John wrote:
skywalk wrote:The settings are in the file as trailing comments.
Yes ... if the preferences are set accordingly. And actually that's the proof that storing the settings in the source file does work. :-)
skywalk wrote:Better if at top in user viewable code.
I disagree. The problem currently is, that those settings are not visible in the part of the code shown in the IDE. That means that the visible part of the source -- which is also e.g. shared here on the forum -- is not self-documenting.
That is not right. The settings listed in the source files at the end of the code are set by the Ide and evaluated exclusively by the Ide. The Ide stores here the settings from the compiler options and when the ide starts the compiler, these settings are passed as parameters. The compiler does not care about these 'notes' in the code.

In any case, not many #PB_Compiler_... settings are suitable for a change in the source code, or a change in the source code is already implemented. Whether this would be possible in the concrete case with ThreadSave can probably only be answered by Fred.
sorry for my bad english
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Compiler Options in source code

Post by skywalk »

Yeah, I would prefer to encapsulate the make file instructions within the source code.
For source control and diff'ing purposes, it really has to be in a separate file.
The trailing comments in individual files is convenient but creates a lot of noise in source code repositories.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Compiler Options in source code

Post by Little John »

Josh wrote:That is not right. The settings listed in the source files at the end of the code are set by the Ide and evaluated exclusively by the Ide. The Ide stores here the settings from the compiler options and when the ide starts the compiler, these settings are passed as parameters. The compiler does not care about these 'notes' in the code.
In the context of what I wrote, it doesn't matter whether the compiler reads those settings directly, or wheter the IDE reads them and passes them to the compiler.
User avatar
Michael Vogel
Addict
Addict
Posts: 2680
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Compiler Options in source code

Post by Michael Vogel »

I would love to have a command like SetCompilerOptions("...") no matter if it would have to be the first line of the code or whatever...

...but also a CheckCompilerOptions("..."), which could be placed into a (generally used) include file could help a little bit - so here's a simple start:

Code: Select all

Procedure CheckCompilerOption(options.s)

	Protected i
	Protected quit
	Protected check
	Protected error.s
	Protected *Char.Character

	options=UCase(options)
	*Char=@options

	Repeat
		Select *Char\c
		Case ' '
		Case 'T'
			check=#PB_Compiler_Thread
			error="Threadsafe"
		Case 'U'
			check=#PB_Compiler_Unicode
			error="Unicode"
		Case '+'
			If check=#Null
				Break
			EndIf
		Case '-'
			If check
				Break
			EndIf
		Case #Null
			quit=#True

		EndSelect
		*Char+SizeOf(Character)
	Until quit

	If quit=0
		If error
			Debug "Set "+error+" compiler option to "+StringField("on. off.",check+1," ")
		Else
			Debug "Unknown compiler switch"
		EndIf
	EndIf

	ProcedureReturn quit

EndProcedure

CheckCompilerOption("T+U-")

; Main program
;      :
;      :

Post Reply