PB-IDE-Tool: Display ASM/C Code (Win,Lin,Mac)

Du brauchst Grafiken, gute Programme oder Leute die dir helfen? Frag hier.
Wolfram
Beiträge: 28
Registriert: 23.08.2013 14:38
Computerausstattung: OSX 10.13 | PB 5.46

Re: PB-IDE-Tool: Display ASM Code (Win,Lin,Mac)

Beitrag von Wolfram »

Ähm… :? Wenn "open" wirklich erforderlich ist, um ein Tool zu starten, dann frage ich mich, warum das PB nicht selber korrekt handhabt.
Was passiert, wenn in Commandline nur der Dateipfad zum Tool und die Parameter in Arguments angegeben werden? Wird das Tool dann nicht aufgerufen?
showAssabler.app ist ein Paket (ein Ordner mit einer Extension) dort kann man keine args übergeben.
Entweder den Befehl auf "/deinPfad/showAssabler.app/Contens/MacOS/showAssabler" ändern oder das Tool als Commandline kompilieren, dann geht es ohne open.

Nur damit der Code sauberer aussieht oder gibt es noch andere Gründe dafür?
So wie Du es geschrieben hast wird der Fensterinhalt verzögert neu berechnet. Mit BindEvend() ist es flüssig.
Ich weiß nicht ob das nur unter OSX so ist, glaube ich aber nicht.
OSX 10.13 | PB 5.46
Benutzeravatar
Sicro
Beiträge: 955
Registriert: 11.08.2005 19:08
Kontaktdaten:

Re: PB-IDE-Tool: Display ASM Code (Win,Lin,Mac)

Beitrag von Sicro »

Danke Wolfram für die Aufklärung. :allright:
Den Code werde ich entsprechend anpassen.
Bild
Warum OpenSource eine Lizenz haben sollte :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (Syntax-Farbschema) :: RegEx-Engine (kompiliert RegExes zu NFA/DFA)
Manjaro Xfce x64 (Hauptsystem) :: Windows 10 Home (VirtualBox) :: Neueste PureBasic-Version
Benutzeravatar
mk-soft
Beiträge: 3695
Registriert: 24.11.2004 13:12
Wohnort: Germany

Re: PB-IDE-Tool: Display ASM Code (Win,Lin,Mac)

Beitrag von mk-soft »

Bei macOS geht es auch wenn man den gesamten Path zum Programm angibt...

Beispiel
/Users/Michael/Daten/Purebasic/displayASM.app/Contents/MacOS/displayASM
Apps... schon geklärt :wink:

P.S. Da ich oft ThreadSafe arbeite, habe ich die Compiler-Option "--thread" hinzugefügt.

Code: Alles auswählen

; Run the PB compiler to create the ASM code file
program = RunProgram(compilerFilePath$,
                     #DQUOTE$ + codeFilePath$ + #DQUOTE$ + " --commented --thread --executable " + #DQUOTE$ + exeFilePath$ + #DQUOTE$,
                     workingDirectoryPath$,
                     #PB_Program_Open | #PB_Program_Read)
P.P.S Habe mir noch den Compiler Output dazu gepackt, falls der Compiler mit Fehler beendet wird

Code: Alles auswählen

; If an error has occurred, output detailed information
If isCompilerError Or asmCode$ = ""
  MessageRequester(#ErrorWindowTitle, "Tool could not create the asm output!" + #CRLF$ +
                                      #CRLF$ + #CRLF$ +
                                      "Compiler File Path:"        + #CRLF$ + compilerFilePath$     + #CRLF$ + #CRLF$ +
                                      "Compiler Working Dir Path:" + #CRLF$ + workingDirectoryPath$ + #CRLF$ + #CRLF$ +
                                      "Code File Path:"            + #CRLF$ + codeFilePath$         + #CRLF$ + #CRLF$ +
                                      "ASM Code File Path:"        + #CRLF$ + asmCodeFilePath$      + #CRLF$ + #CRLF$ +
                                      "EXE File Path:"             + #CRLF$ + exeFilePath$          + #CRLF$ + #CRLF$ +
                                      "Compiler Output:"            + #CRLF$ + compilerOutput$,
                   #PB_MessageRequester_Error)
  End
EndIf
Danke fürs Tool :allright:
Alles ist möglich, fragt sich nur wie...
Projekte ThreadToGUI / EventDesigner V3 / OOP-BaseClass-Modul
Downloads auf MyWebspace / OneDrive
Benutzeravatar
Sicro
Beiträge: 955
Registriert: 11.08.2005 19:08
Kontaktdaten:

Re: PB-IDE-Tool: Display ASM Code (Win,Lin,Mac)

Beitrag von Sicro »

Danke @Wolfram und @mk-soft für die Hinweise und Verbesserungsvorschläge :allright:

Changelog:
  • Use module 'OpenStandardProgram'
  • Use 'BindEvent()' for '#PB_Event_SizeWindow' events
  • Use 'PB_Tool_Compiler' instead of '%HOME'
  • Use compiler options of the current code
  • Add MacOS 'Application' menu support
  • Add more information to the error output
  • Add more information to the ASM output
  • Remove ASM file after reading
  • Add a note for MacOS to the setting descriptions of the PB-IDE-Tools
Bild
Warum OpenSource eine Lizenz haben sollte :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (Syntax-Farbschema) :: RegEx-Engine (kompiliert RegExes zu NFA/DFA)
Manjaro Xfce x64 (Hauptsystem) :: Windows 10 Home (VirtualBox) :: Neueste PureBasic-Version
Benutzeravatar
mk-soft
Beiträge: 3695
Registriert: 24.11.2004 13:12
Wohnort: Germany

Re: PB-IDE-Tool: Display ASM Code (Win,Lin,Mac)

Beitrag von mk-soft »

Alles ist möglich, fragt sich nur wie...
Projekte ThreadToGUI / EventDesigner V3 / OOP-BaseClass-Modul
Downloads auf MyWebspace / OneDrive
Benutzeravatar
mk-soft
Beiträge: 3695
Registriert: 24.11.2004 13:12
Wohnort: Germany

Re: PB-IDE-Tool: Display ASM Code (Win,Lin,Mac)

Beitrag von mk-soft »

Hallo Sicro,

Für C-backend angepasst.

Update 28.11.2021
- Angepasst für C-Code
- Überprüfung und Korrektur Environment Variable "PB_TOOL_Compiler"
- PB Editor Standard Konstanten hinzugefügt
- Ausgabe in Standard Editor und Zwischenablage den C-Code ohne eigenen Header
- Include Dateien Abhängigkeiten aufgelöst

Update 20.12.2021
- Bei Arm32/64 ist Compiler name "pbcompiler". Auch wenn es ein immer C-Backend ist

Update 01.06.2022
- Ausgabe Compiler Output im Fenster
- Linux Fix Wayland

Update 06.10.2023
- Remove Fix Wayland
- Update Output

displayCcode.pb

Code: Alles auswählen

;-Description   : Displays the commented C code from the PB compiler
; OS            : Windows, Linux, Mac
; English-Forum :
; French-Forum  :
; German-Forum  : https://www.purebasic.fr/german/viewtopic.php?f=10&t=30935
; 
; Author        : Sicro
; Author second : mk-soft (Modified for C-Code)
; Update        : 06.10.2023

; -----------------------------------------------------------------------------

; MIT License
;
; Copyright (c) 2018-2020 Sicro
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in all
; copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.

; Tool Settings:
; - Arguments: "%FILE" "%TEMPFILE"
; - Event:     Menu Or Shortcut
; For MacOS, the field "Commandline" must contain the full path to the executable
; file, e.g.: .../Program.app/Contents/MacOS/Program

EnableExplicit

; =============
;-Include Codes
; =============

;XIncludeFile "../System/OpenStandardProgram.pbi"

Procedure.i OpenWithStandardProgram(FilePath$)
  
  Protected Result
  
  ; Avoid problems with paths containing spaces
  FilePath$ = #DQUOTE$ + FilePath$ + #DQUOTE$
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      ; https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/nf-shellapi-shellexecutew
      Result = Bool(ShellExecute_(0, "open", FilePath$, #Null, #Null, #SW_SHOW) > 32)
    CompilerCase #PB_OS_Linux
      ; https://portland.freedesktop.org/doc/xdg-open.html
      Result = Bool(RunProgram("xdg-open", FilePath$, GetCurrentDirectory()))
    CompilerCase #PB_OS_MacOS
      Result = Bool(RunProgram("open", FilePath$, GetCurrentDirectory()))
  CompilerEndSelect
  
  ProcedureReturn Result
  
EndProcedure

; ================
;-Define Constants
; ================

CompilerIf Not Defined(PB_Processor_Arm32, #PB_Constant)
  #PB_Processor_Arm32 = 7
CompilerEndIf

CompilerIf Not Defined(PB_Processor_Arm64, #PB_Constant)
  #PB_Processor_Arm64 = 6
CompilerEndIf

#Program_Name     = "Display C-Code"
#ErrorWindowTitle = #Program_Name + " - ERROR"

#Window_Main = 0

Enumeration Gadget
  #Editor_Output
  #Button_OpenStandardEditor
  #Button_CopyToClipboard
EndEnumeration

; ======================
;-Define Local Variables
; ======================

Define compilerHomePath$, compilerFilePath$, workingDirectoryPath$, codeFilePath$, codeTempFilePath$,
       cCodeFilePath$, exeFilePath$, outputFilePathForStandardProgram$, compilerParameters$,
       compilerUserParameters$
Define cCode$, output$, compilerOutput$
Define program, file, event, isCompilerError, countOfParameters, i, isLibrary

Global compilerVersion$, isCompilerV6

; ==============================
;-Set Values For Local Variables
; ==============================

outputFilePathForStandardProgram$ = GetTemporaryDirectory() + RemoveString(#Program_Name, " ") + "-Output.txt"

compilerFilePath$ = GetEnvironmentVariable("PB_TOOL_Compiler")
If compilerFilePath$ = ""
  MessageRequester("Info", "Run only as PB-IDE Tool", #PB_MessageRequester_Info)
  End
EndIf

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  If Not FindString(compilerFilePath$, "pbcompilerc")
    compilerFilePath$ = ReplaceString(compilerFilePath$, "pbcompiler", "pbcompilerc")
  EndIf
CompilerElse
  If FileSize(compilerFilePath$ + "c") > 0
    compilerFilePath$ = ReplaceString(compilerFilePath$, "pbcompiler", "pbcompilerc")
  EndIf
CompilerEndIf

; Run the PB compiler to get compiler version
program = RunProgram(compilerFilePath$, "-v", "", #PB_Program_Open | #PB_Program_Read)

; Read the PB compiler output compiler version
If program
  While ProgramRunning(program)
    While AvailableProgramOutput(program)
      compilerVersion$ + ReadProgramString(program) + #CRLF$
    Wend
  Wend
  isCompilerError = Bool(ProgramExitCode(program))
  CloseProgram(program)
Else
  isCompilerError = #True
EndIf

If FindString(compilerVersion$, "PureBasic 6.")
  isCompilerV6 = #True
EndIf

compilerHomePath$ = GetPathPart(compilerFilePath$)
codeFilePath$     = ProgramParameter(0) ; "%FILE"
codeTempFilePath$ = ProgramParameter(1) ; "%TEMPFILE"

If codeFilePath$ = ""
  ; The code has not yet been saved
  ; Use the path of the auto-generated temp code file
  codeFilePath$ = codeTempFilePath$
EndIf

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  cCodeFilePath$ = compilerHomePath$ + "purebasic.c"
CompilerElse
  cCodeFilePath$ = GetPathPart(codeFilePath$) + "purebasic.c"
CompilerEndIf

; The EXE file is only created so that the PB compiler does not execute the code, but only creates the ASM code
exeFilePath$ = GetTemporaryDirectory() + "purebasic"

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  workingDirectoryPath$ = GetPathPart(compilerFilePath$)
CompilerElse
  workingDirectoryPath$ = GetPathPart(codeFilePath$)
CompilerEndIf

compilerParameters$ = "--commented"

; Parameter for constants change since pb v6.00
If isCompilerV6
  compilerParameters$ + " -co PB_Editor_BuildCount=0 -co PB_Editor_CompileCount=0"
Else
  compilerParameters$ + " -o PB_Editor_BuildCount=0 -o PB_Editor_CompileCount=0"
EndIf

If Val(GetEnvironmentVariable("PB_TOOL_Thread"))
  compilerParameters$ + " --thread"
EndIf

If Val(GetEnvironmentVariable("PB_TOOL_Unicode"))
  compilerParameters$ + " --unicode"
EndIf

If GetEnvironmentVariable("PB_TOOL_SubSystem")
  compilerParameters$ + " --subsystem " + GetEnvironmentVariable("PB_TOOL_SubSystem")
EndIf

Procedure$ ProcessParameter(parameter$, exeFilePath$, *isLibrary.Integer)
  Select LCase(parameter$)
    Case "-dl", "--dylib", "-so", "--sharedobject"
      parameter$ = parameter$ + " " + #DQUOTE$ + exeFilePath$ + #DQUOTE$
      *isLibrary\i = #True
    Case "/dll"
      If isCompilerV6
        parameter$ = "--output " + #DQUOTE$ + exeFilePath$ + #DQUOTE$ + parameter$
      Else
        parameter$ = "--executable " + #DQUOTE$ + exeFilePath$ + #DQUOTE$ + parameter$
      EndIf
      *isLibrary\i = #True
  EndSelect
  ProcedureReturn parameter$
EndProcedure

; Get user parameters
countOfParameters = CountProgramParameters()
For i = 2 To countOfParameters - 1
  compilerUserParameters$ + " " + ProcessParameter(ProgramParameter(i), exeFilePath$, @isLibrary)
Next
If Not isLibrary
  If isCompilerV6
    compilerParameters$ + " --output " + #DQUOTE$ + exeFilePath$ + #DQUOTE$
  Else
    compilerParameters$ + " --executable " + #DQUOTE$ + exeFilePath$ + #DQUOTE$
  EndIf
EndIf
compilerParameters$ + compilerUserParameters$

; =======================
;-Delete Old Output Files
; =======================

; Make sure that there are no previous output files, to prevent old output files from being read, if an error occurs.
DeleteFile(cCodeFilePath$)
DeleteFile(exeFilePath$)

; ====================
;-Create ASM Code File
; ====================

; Run the PB compiler to create the ASM code file
program = RunProgram(compilerFilePath$,
                     #DQUOTE$ + codeFilePath$ + #DQUOTE$ + " " + compilerParameters$,
                     workingDirectoryPath$,
                     #PB_Program_Open | #PB_Program_Read)

; Read the PB compiler output
If program
  While ProgramRunning(program)
    While AvailableProgramOutput(program)
      compilerOutput$ + ReadProgramString(program) + #CRLF$
    Wend
  Wend
  isCompilerError = Bool(ProgramExitCode(program))
  CloseProgram(program)
Else
  isCompilerError = #True
EndIf

; Read the ASM code file
file = ReadFile(#PB_Any, cCodeFilePath$)
If file
  cCode$ + ReadString(file, #PB_File_IgnoreEOL)
  CloseFile(file)
EndIf

; If an error has occurred, output detailed information
If isCompilerError Or cCode$ = ""
  MessageRequester(#ErrorWindowTitle, "Tool could not create the asm output!" +
                                      #CRLF$ + #CRLF$ +
                                      "Note that the PB compiler only generates ASM output if the PB code is syntaxically correct!" +
                                      #CRLF$ + #CRLF$ +
                                      "-------------------------------------" +
                                      #CRLF$ + #CRLF$ +
                                      "Compiler File Path:"        + #CRLF$ + compilerFilePath$     + #CRLF$ + #CRLF$ +
                                      "Compiler Parameters:"       + #CRLF$ + compilerParameters$   + #CRLF$ + #CRLF$ +
                                      "Compiler Working Dir Path:" + #CRLF$ + workingDirectoryPath$ + #CRLF$ + #CRLF$ +
                                      "C-Code File Path:"          + #CRLF$ + cCodeFilePath$        +
                                      #CRLF$ + #CRLF$ +
                                      "-------------------------------------" +
                                      #CRLF$ + #CRLF$ +
                                      "Compiler Output:"           + #CRLF$ + compilerOutput$,
                   #PB_MessageRequester_Error)
  ;End
EndIf

; Remove the ASM file only if no error occurred to avoid removing error traces
DeleteFile(exeFilePath$)

output$ = "*** DisplayCcode ***"                             + #CRLF$ +
          #CRLF$ +
          "- Compiler File Path: "  + compilerFilePath$      + #CRLF$ +
          "- Compiler Parameters: " + compilerParameters$    + #CRLF$ +
          "- C-Code File Path:"     + cCodeFilePath$         + #CRLF$ +
          #CRLF$ +
          "*** Compiler ***"                                 + #CRLF$ +
          #CRLF$ +
          compilerOutput$                                    + #CRLF$ +
          "*** C-Code ***"                                   + #CRLF$ +
          cCode$

; =============================
;-Create ASM Code Output Window
; =============================

Procedure UpdateWindow()
  ResizeGadget(#Editor_Output, #PB_Ignore, #PB_Ignore, WindowWidth(#Window_Main), WindowHeight(#Window_Main) - 40)
  ResizeGadget(#Button_OpenStandardEditor, 5, GadgetHeight(#Editor_Output) + 5, WindowWidth(#Window_Main) / 2 - 8, #PB_Ignore)
  ResizeGadget(#Button_CopyToClipboard, GadgetWidth(#Button_OpenStandardEditor) + 10, GadgetHeight(#Editor_Output) + 5,
               WindowWidth(#Window_Main) / 2 - 8, #PB_Ignore)
EndProcedure
 
; Display the ASM code in an EditorGadget
If Not OpenWindow(#Window_Main, #PB_Ignore, #PB_Ignore, 500, 500, #Program_Name, #PB_Window_SystemMenu | #PB_Window_SizeGadget |
                                                                                 #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  MessageRequester(#ErrorWindowTitle, "The program window could not be created!", #PB_MessageRequester_Error)
  End
EndIf

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
  If Not IsMenu(#Window_Main)
    CreateMenu(0, WindowID(#Window_Main))
  EndIf
  
  ;MenuItem(#PB_Menu_About, "")
  ;MenuItem(#PB_Menu_Preferences, "")
  MenuItem(#PB_Menu_Quit, "")
CompilerEndIf

EditorGadget(#Editor_Output, 0, 0, WindowWidth(#Window_Main), WindowHeight(#Window_Main) - 40)
ButtonGadget(#Button_OpenStandardEditor, 5, GadgetHeight(#Editor_Output) + 5, WindowWidth(#Window_Main) / 2 - 8, 30, "Open Standard Editor")
ButtonGadget(#Button_CopyToClipboard, GadgetWidth(#Button_OpenStandardEditor) + 10, GadgetHeight(#Editor_Output) + 5,
             WindowWidth(#Window_Main) / 2 - 8, 30, "Copy To Clipboard")
SetGadgetText(#Editor_Output, output$)

BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Window_Main)

Repeat
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Button_OpenStandardEditor
          If Not OpenWithStandardProgram(cCodeFilePath$)
            file = CreateFile(#PB_Any, outputFilePathForStandardProgram$)
            If file
              WriteStringN(file, cCode$)
              CloseFile(file)
            EndIf
            If Not OpenWithStandardProgram(outputFilePathForStandardProgram$)
              MessageRequester(#ErrorWindowTitle, "The output could not be opened in the standard editor!", #PB_MessageRequester_Error)
            EndIf
          EndIf
        Case #Button_CopyToClipboard
          SetClipboardText(cCode$)
      EndSelect
    Case #PB_Event_Menu
      CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
        ; MacOS 'Application' menu
        Select EventMenu()
          Case #PB_Menu_About
          Case #PB_Menu_Preferences
          Case #PB_Menu_Quit
            Break
        EndSelect
      CompilerEndIf
  EndSelect
Until event = #PB_Event_CloseWindow
Zuletzt geändert von mk-soft am 04.01.2024 21:02, insgesamt 6-mal geändert.
Alles ist möglich, fragt sich nur wie...
Projekte ThreadToGUI / EventDesigner V3 / OOP-BaseClass-Modul
Downloads auf MyWebspace / OneDrive
Benutzeravatar
mk-soft
Beiträge: 3695
Registriert: 24.11.2004 13:12
Wohnort: Germany

Re: PB-IDE-Tool: Display ASM Code (Win,Lin,Mac)

Beitrag von mk-soft »

Update 28.11.2021
- Angepasst für ASM-Code
- Überprüfung und Korrektur Environment Variable "PB_TOOL_Compiler"
- PB Editor Standard Konstanten hinzugefügt
- Ausgabe in Standard Editor und Zwischenablage den asmCode ohne eigenen Header
- Include Dateien Abhängigkeiten aufgelöst

Update 01.06.2022
- Linux Fix Wayland

Update 06.10.2023
- Remove Fix Wayland
- Update Output

displayASMcode.pb

Code: Alles auswählen

;-Description   : Displays the commented ASM code from the PB compiler
; OS            : Windows, Linux, Mac
; English-Forum :
; French-Forum  :
; German-Forum  : https://www.purebasic.fr/german/viewtopic.php?f=10&t=30935
; 
; Author        : Sicro
; Author second : mk-soft
; Update        : 06.10.2023

; -----------------------------------------------------------------------------

; MIT License
;
; Copyright (c) 2018-2020 Sicro
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in all
; copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.

; Tool Settings:
; - Arguments: "%FILE" "%TEMPFILE"
; - Event:     Menu Or Shortcut
; For MacOS, the field "Commandline" must contain the full path to the executable
; file, e.g.: .../Program.app/Contents/MacOS/Program

EnableExplicit

; =============
;-Include Codes
; =============

;XIncludeFile "../System/OpenStandardProgram.pbi"

Procedure.i OpenWithStandardProgram(FilePath$)
  
  Protected Result
  
  ; Avoid problems with paths containing spaces
  FilePath$ = #DQUOTE$ + FilePath$ + #DQUOTE$
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      ; https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/nf-shellapi-shellexecutew
      Result = Bool(ShellExecute_(0, "open", FilePath$, #Null, #Null, #SW_SHOW) > 32)
    CompilerCase #PB_OS_Linux
      ; https://portland.freedesktop.org/doc/xdg-open.html
      Result = Bool(RunProgram("xdg-open", FilePath$, GetCurrentDirectory()))
    CompilerCase #PB_OS_MacOS
      Result = Bool(RunProgram("open", FilePath$, GetCurrentDirectory()))
  CompilerEndSelect
  
  ProcedureReturn Result
  
EndProcedure

; ================
;-Define Constants
; ================

#Program_Name     = "Display ASM Code"
#ErrorWindowTitle = #Program_Name + " - ERROR"

#Window_Main = 0

Enumeration Gadget
  #Editor_Output
  #Button_OpenStandardEditor
  #Button_CopyToClipboard
EndEnumeration

; ======================
;-Define Local Variables
; ======================

Define compilerHomePath$, compilerFilePath$, workingDirectoryPath$, codeFilePath$, codeTempFilePath$,
       asmCodeFilePath$, exeFilePath$, outputFilePathForStandardProgram$, compilerParameters$,
       compilerUserParameters$
Define asmCode$, output$, compilerOutput$
Define program, file, event, isCompilerError, countOfParameters, i, isLibrary

Global compilerVersion$, isCompilerV6

; ==============================
;-Set Values For Local Variables
; ==============================

outputFilePathForStandardProgram$ = GetTemporaryDirectory() + RemoveString(#Program_Name, " ") + "-Output.txt"

compilerFilePath$ = GetEnvironmentVariable("PB_TOOL_Compiler")
If compilerFilePath$ = ""
  MessageRequester("Info", "Run only as PB-IDE Tool", #PB_MessageRequester_Info)
  End
EndIf

If FindString(compilerFilePath$, "pbcompilerc")
  compilerFilePath$   = ReplaceString(compilerFilePath$, "pbcompilerc", "pbcompiler")
EndIf

If FileSize(compilerFilePath$ + "asm") > 0
  compilerFilePath$   = ReplaceString(compilerFilePath$, "pbcompiler", "pbcompilerasm")
EndIf

; Run the PB compiler to get compiler version
program = RunProgram(compilerFilePath$, "-v", "", #PB_Program_Open | #PB_Program_Read)

; Read the PB compiler output compiler version
If program
  While ProgramRunning(program)
    While AvailableProgramOutput(program)
      compilerVersion$ + ReadProgramString(program) + #CRLF$
    Wend
  Wend
  isCompilerError = Bool(ProgramExitCode(program))
  CloseProgram(program)
Else
  isCompilerError = #True
EndIf

If FindString(compilerVersion$, "PureBasic 6.")
  isCompilerV6 = #True
EndIf

compilerHomePath$ = GetPathPart(compilerFilePath$)
codeFilePath$     = ProgramParameter(0) ; "%FILE"
codeTempFilePath$ = ProgramParameter(1) ; "%TEMPFILE"

If codeFilePath$ = ""
  ; The code has not yet been saved
  ; Use the path of the auto-generated temp code file
  codeFilePath$ = codeTempFilePath$
EndIf

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  asmCodeFilePath$ = compilerHomePath$ + "purebasic.asm"
CompilerElse
  asmCodeFilePath$ = GetPathPart(codeFilePath$) + "purebasic.asm"
CompilerEndIf

; The EXE file is only created so that the PB compiler does not execute the code, but only creates the ASM code
exeFilePath$ = GetTemporaryDirectory() + "purebasic"

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  workingDirectoryPath$ = GetPathPart(compilerFilePath$)
CompilerElse
  workingDirectoryPath$ = GetPathPart(codeFilePath$)
CompilerEndIf

compilerParameters$ = "--commented"

; Parameter for constants change since pb v6.00
If isCompilerV6
  compilerParameters$ + " -co PB_Editor_BuildCount=0 -co PB_Editor_CompileCount=0"
Else
  compilerParameters$ + " -o PB_Editor_BuildCount=0 -o PB_Editor_CompileCount=0"
EndIf

If Val(GetEnvironmentVariable("PB_TOOL_Thread"))
  compilerParameters$ + " --thread"
EndIf

If Val(GetEnvironmentVariable("PB_TOOL_Unicode"))
  compilerParameters$ + " --unicode"
EndIf

If GetEnvironmentVariable("PB_TOOL_SubSystem")
  compilerParameters$ + " --subsystem " + GetEnvironmentVariable("PB_TOOL_SubSystem")
EndIf

Procedure$ ProcessParameter(parameter$, exeFilePath$, *isLibrary.Integer)
  Select LCase(parameter$)
    Case "-dl", "--dylib", "-so", "--sharedobject"
      parameter$ = parameter$ + " " + #DQUOTE$ + exeFilePath$ + #DQUOTE$
      *isLibrary\i = #True
    Case "/dll"
      If isCompilerV6
        parameter$ = "--output " + #DQUOTE$ + exeFilePath$ + #DQUOTE$ + parameter$
      Else
        parameter$ = "--executable " + #DQUOTE$ + exeFilePath$ + #DQUOTE$ + parameter$
      EndIf
      *isLibrary\i = #True
  EndSelect
  ProcedureReturn parameter$
EndProcedure

; Get user parameters
countOfParameters = CountProgramParameters()
For i = 2 To countOfParameters - 1
  compilerUserParameters$ + " " + ProcessParameter(ProgramParameter(i), exeFilePath$, @isLibrary)
Next
If Not isLibrary
  If isCompilerV6
    compilerParameters$ + " --output " + #DQUOTE$ + exeFilePath$ + #DQUOTE$
  Else
    compilerParameters$ + " --executable " + #DQUOTE$ + exeFilePath$ + #DQUOTE$
  EndIf
EndIf
compilerParameters$ + compilerUserParameters$

; =======================
;-Delete Old Output Files
; =======================

; Make sure that there are no previous output files, to prevent old output files from being read, if an error occurs.
DeleteFile(asmCodeFilePath$)
DeleteFile(exeFilePath$)

; ====================
;-Create ASM Code File
; ====================

; Run the PB compiler to create the ASM code file
program = RunProgram(compilerFilePath$,
                     #DQUOTE$ + codeFilePath$ + #DQUOTE$ + " " + compilerParameters$,
                     workingDirectoryPath$,
                     #PB_Program_Open | #PB_Program_Read)

; Read the PB compiler output
If program
  While ProgramRunning(program)
    While AvailableProgramOutput(program)
      compilerOutput$ + ReadProgramString(program) + #CRLF$
    Wend
  Wend
  isCompilerError = Bool(ProgramExitCode(program))
  CloseProgram(program)
Else
  isCompilerError = #True
EndIf

; Read the ASM code file
file = ReadFile(#PB_Any, asmCodeFilePath$)
If file
  asmCode$ + ReadString(file, #PB_File_IgnoreEOL)
  CloseFile(file)
EndIf

; If an error has occurred, output detailed information
If isCompilerError Or asmCode$ = ""
  MessageRequester(#ErrorWindowTitle, "Tool could not create the asm output!" +
                                      #CRLF$ + #CRLF$ +
                                      "Note that the PB compiler only generates ASM output if the PB code is syntaxically correct!" +
                                      #CRLF$ + #CRLF$ +
                                      "-------------------------------------" +
                                      #CRLF$ + #CRLF$ +
                                      "Compiler File Path:"        + #CRLF$ + compilerFilePath$     + #CRLF$ + #CRLF$ +
                                      "Compiler Parameters:"       + #CRLF$ + compilerParameters$   + #CRLF$ + #CRLF$ +
                                      "Compiler Working Dir Path:" + #CRLF$ + workingDirectoryPath$ + #CRLF$ + #CRLF$ +
                                      "ASM Code File Path:"        + #CRLF$ + asmCodeFilePath$      +
                                      #CRLF$ + #CRLF$ +
                                      "-------------------------------------" +
                                      #CRLF$ + #CRLF$ +
                                      "Compiler Output:"           + #CRLF$ + compilerOutput$,
                   #PB_MessageRequester_Error)
  ;End
EndIf

; Remove the ASM file only if no error occurred to avoid removing error traces
DeleteFile(exeFilePath$)

output$ = "*** DisplayASMcode ***"                         + #CRLF$ + 
          #CRLF$ +
          "- Compiler File Path: "  + compilerFilePath$    + #CRLF$ +
          "- Compiler Parameters: " + compilerParameters$  + #CRLF$ +
          "- ASM Code File Path:"   + asmCodeFilePath$     + #CRLF$ +
          #CRLF$ +
          "*** Compiler ***"                               + #CRLF$ +
          #CRLF$ +
          compilerOutput$                                  + #CRLF$ +
          "*** ASM-Code ***"                               + #CRLF$ +
          asmCode$

; =============================
;-Create ASM Code Output Window
; =============================

Procedure UpdateWindow()
  ResizeGadget(#Editor_Output, #PB_Ignore, #PB_Ignore, WindowWidth(#Window_Main), WindowHeight(#Window_Main) - 40)
  ResizeGadget(#Button_OpenStandardEditor, 5, GadgetHeight(#Editor_Output) + 5, WindowWidth(#Window_Main) / 2 - 8, #PB_Ignore)
  ResizeGadget(#Button_CopyToClipboard, GadgetWidth(#Button_OpenStandardEditor) + 10, GadgetHeight(#Editor_Output) + 5,
               WindowWidth(#Window_Main) / 2 - 8, #PB_Ignore)
EndProcedure
 
; Display the ASM code in an EditorGadget
If Not OpenWindow(#Window_Main, #PB_Ignore, #PB_Ignore, 500, 500, #Program_Name, #PB_Window_SystemMenu | #PB_Window_SizeGadget |
                                                                                 #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  MessageRequester(#ErrorWindowTitle, "The program window could not be created!", #PB_MessageRequester_Error)
  End
EndIf

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
  If Not IsMenu(#Window_Main)
    CreateMenu(0, WindowID(#Window_Main))
  EndIf
  
  ;MenuItem(#PB_Menu_About, "")
  ;MenuItem(#PB_Menu_Preferences, "")
  MenuItem(#PB_Menu_Quit, "")
CompilerEndIf

EditorGadget(#Editor_Output, 0, 0, WindowWidth(#Window_Main), WindowHeight(#Window_Main) - 40)
ButtonGadget(#Button_OpenStandardEditor, 5, GadgetHeight(#Editor_Output) + 5, WindowWidth(#Window_Main) / 2 - 8, 30, "Open Standard Editor")
ButtonGadget(#Button_CopyToClipboard, GadgetWidth(#Button_OpenStandardEditor) + 10, GadgetHeight(#Editor_Output) + 5,
             WindowWidth(#Window_Main) / 2 - 8, 30, "Copy To Clipboard")
SetGadgetText(#Editor_Output, output$)

BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Window_Main)

Repeat
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Button_OpenStandardEditor
          file = CreateFile(#PB_Any, outputFilePathForStandardProgram$)
          If file
            WriteStringN(file, asmCode$)
            CloseFile(file)
          EndIf
          If Not OpenWithStandardProgram(outputFilePathForStandardProgram$)
            MessageRequester(#ErrorWindowTitle, "The output could not be opened in the standard editor!", #PB_MessageRequester_Error)
          EndIf
        Case #Button_CopyToClipboard
          SetClipboardText(asmCode$)
      EndSelect
    Case #PB_Event_Menu
      CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
        ; MacOS 'Application' menu
        Select EventMenu()
          Case #PB_Menu_About
          Case #PB_Menu_Preferences
          Case #PB_Menu_Quit
            Break
        EndSelect
      CompilerEndIf
  EndSelect
Until event = #PB_Event_CloseWindow
Zuletzt geändert von mk-soft am 04.01.2024 21:01, insgesamt 5-mal geändert.
Alles ist möglich, fragt sich nur wie...
Projekte ThreadToGUI / EventDesigner V3 / OOP-BaseClass-Modul
Downloads auf MyWebspace / OneDrive
Benutzeravatar
Sicro
Beiträge: 955
Registriert: 11.08.2005 19:08
Kontaktdaten:

Re: PB-IDE-Tool: Display ASM Code (Win,Lin,Mac)

Beitrag von Sicro »

@mk-soft
Danke für deine Anpassungen. :-)
Wie ich auf GitHub geschrieben habe, sollten wir alles in einem einzigen Tool vereinen, nachdem der PB_TOOL_Compiler-Bug behoben ist.

Wenn es so weit ist, melde ich mich nochmal bei dir.
Bild
Warum OpenSource eine Lizenz haben sollte :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (Syntax-Farbschema) :: RegEx-Engine (kompiliert RegExes zu NFA/DFA)
Manjaro Xfce x64 (Hauptsystem) :: Windows 10 Home (VirtualBox) :: Neueste PureBasic-Version
Benutzeravatar
mk-soft
Beiträge: 3695
Registriert: 24.11.2004 13:12
Wohnort: Germany

Re: PB-IDE-Tool: Display ASM Code (Win,Lin,Mac)

Beitrag von mk-soft »

Update DisplayCcode 20.12.2021
- Bei Arm32/64 ist der Compiler Name immer "pbcompiler". Auch wenn es ein C-Backend ist

Läuft auch auf einem Paspberry PI ;)
Alles ist möglich, fragt sich nur wie...
Projekte ThreadToGUI / EventDesigner V3 / OOP-BaseClass-Modul
Downloads auf MyWebspace / OneDrive
Axolotl
Beiträge: 146
Registriert: 31.12.2008 16:34

Re: PB-IDE-Tool: Display ASM Code (Win,Lin,Mac)

Beitrag von Axolotl »

Moin Sicro und mk-soft,

ich habe mal euer Tool ausprobiert. Danke fürs Teilen.
Bei mir läuft es nur wenn ich die folgenden Änderungen vornehme...
Unter Windows kann ich als Std-User ja nicht mehr in das Compiler-Verzeichnis schreiben.
A.m.S. ist an beiden Stellen eine OS-Unterscheidung also gar nicht erforderlich.

Code: Alles auswählen

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
; cCodeFilePath$ = compilerHomePath$ + "purebasic.c"           ; cannot write to C:\Programme\PureBasic\  
  cCodeFilePath$ = GetPathPart(codeFilePath$) + "purebasic.c"  ; #### this works on my computer 
CompilerElse
  cCodeFilePath$ = GetPathPart(codeFilePath$) + "purebasic.c"
CompilerEndIf

; The EXE file is only created so that the PB compiler does not execute the code, but only creates the ASM code
exeFilePath$ = GetTemporaryDirectory() + "purebasic"

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
; workingDirectoryPath$ = GetPathPart(compilerFilePath$)      ; cannot write to C:\Programme\PureBasic\  
  workingDirectoryPath$ = GetPathPart(exeFilePath$)           ; #### this works on my computer 
CompilerElse
  workingDirectoryPath$ = GetPathPart(codeFilePath$)
CompilerEndIf


Update: Gleiches gilt auch für die Assembler Version.
Update-2: Bisheriges Verhalten mit <New> Datei ermittelt.
Mit abespeicherter Datei Test.pb gibt es wieder die Fehlermeldung, obwohl die Dateien (purebasic und purebasic.c) im Temp Verzeichnis angelegt werden.
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
Antworten