Author Topic: QB64 Command Line Compiler Script  (Read 2851 times)

0 Members and 1 Guest are viewing this topic.

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
QB64 Command Line Compiler Script
« on: June 27, 2021, 12:15:23 pm »
EDITED 6/27/21: I updated the script (now version 1.1) to allow -c "--help" to execute the help option of the compiler. This will help adding in the replacement compiler option function.

This script will compile a QB64 program via the terminal (command line). It allows you to pass additional compiler switches as well as the directory and program source.

This script runs on Linux, and @SpriggsySpriggs has offered to create a Window's version of this. I will be testing this script on macOS, and will post that script here when it's done.

Some usage conventions are required to use this script:
   1) You need to update your PATH variable to where the qb64 binary resides (in Linux, it is in the /etc/environment file)
   2) Currently this may not work as ROOT for Linux users (There are issues if qb64 is executed from a ROOT directory)
   
This is what my file "environment" looks like with the qb64 directory added:
   PATH="/home/gjmcginn/qb64:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
   
Once you update your PATH, you will need to reboot your system for the changes to take effect.
   
This script will take up to 3 arguments, each proceeded by a switch, which tells the script where to place them. The switches are:
   -c (Provide additional compiler options besides the -c and -o)
   -d (The directory where both the binary will be created and where the source file is located)
   -p (The source file without the .bas extension - the script adds this)
   
   Note: The script has a -r switch. This is to replace the compiler options and will be implemented in Version 2.
   
The default compiler switches applied to all compiles are: -c -x and -o.
   -x                         Compile instead of edit and output the result to the console
   -o <output file>    Write output executable to <output file>

To see all the switches for qb64, you can use just "-c --help" with this script and it will display them in the terminal program.
   
The syntax for executing this script is:
   compileqb64.sh -c "compiler options" -d "source directory" -p "source code"

For example:
   compileqb64.sh -c "-w -e" -d "/home/gjmcginn/SourceCode/basic" -p "EBAC Calculator"
   
This example adds -w and -e to the compiler options, sets up the directory, and provides the <file> to be compiled. Things to remember:
   1) If there are more than one compiler options, they must be enclosed in quotes
   2) If either your directory or file name has spaces, they too must be enclosed in quotes. (You can always use quotes,regardless)
   3) Do not add the / at the end of the directory name from -d. The script will do this for you.

The script will do some rudimentary error checking, and will display in the terminal program its progress. The script is well commented.

Save as compileqb64.sh (Or any name you wish to run it as):
Code: Bash: [Select]
  1. #!/bin/bash
  2.  
  3. #QB64 Compiler -- Shell Script -- George McGinn 2021
  4. #Version 1.5 -- June 28, 2021
  5. #
  6. # compileqb64 - script to compile QB64 source code from any directory
  7. #
  8. # This script takes in up to 3 arguments for use in compiling QB64 from
  9. # the command line. At least two arguments must be present to compile,
  10. # or the script will not work right. The arguments passed are:
  11. #       -c Compiler Options (Add to default) (In quotes)
  12. #   -r Compiler Options (Replace default) (In quotes)
  13. #       -d Source Code/Object Directory Name
  14. #       -p Program File Name (without the .bas extension)
  15. #
  16. # Syntax for compileqb64 is:
  17. #       compileqb64 -c "compiler options" -d "source directory" -p "source code"
  18. # or:
  19. #   compileqb64 -c "--help"
  20. #
  21. # By using switches (-a, -d, -p) this script will parse out the argument
  22. # values, and load the variables based on the values passed. Note that the
  23. # script will append the .bas to the source code <filename>.
  24. #
  25. # Both switches -c and -r populate compiler options, but in a different way.
  26. # The -c switch tells the script to "append" the arguments passed to the ones
  27. # used as default (the -c and -o).
  28. #
  29. # The -r tells the script to replace the default with the ones provided. This may be
  30. # used if you want to compile only to get the C++ code, or start the IDE with or
  31. # without a program loaded.
  32. #
  33. # If you use -c "--help" then the compiler will display the HELP text.
  34. #
  35. # The script, based on what was passed, will build and execute the compile statment.
  36. # The script does a check to see if the source file exists. If it does, it completes
  37. # the compile. If the directory or file does not exist, it errors out.
  38. #
  39. # The process in Version 1.x cannot run as ROOT and will need to be tested.
  40. #
  41.  
  42. # FUNCTION: Test to see if the source code exists (True if the FILE exists and is a regular file (not a directory or device))
  43. is_file() {
  44.         if [ ! -f $FILE ];    
  45.         then
  46.                 echo && echo "*** ERROR: $FILE does not exist. Please fix and retry. Script Terminated."
  47.                 exit 2
  48.         fi
  49. }
  50.  
  51. ### Start of script
  52.  
  53. # Make sure we're not running as root (To be tested for future versions)
  54. if [ $EUID == "0" ]; then
  55.   echo "*** ERROR: You are trying to run this script as root. This is highly unrecommended. Script Terminated."
  56.   exit 1
  57. fi
  58.  
  59. echo "Starting QB64 Command Line Compiler..."
  60. echo
  61.  
  62. # Display the number of arguments passed and their values
  63. echo "Number of arguments passed: $#"
  64. echo "Arguments: $@"
  65.  
  66. # If no compiler options are found, display error message, qb64 help and terminate
  67. if [ $# == 0 ]; then                                                                                           
  68.         echo "*** ERROR: No Compiler Options passed. Make sure you use one of the ones below. Script terminated."
  69.         echo "qb64 --help"
  70.         echo
  71.         qb64 --help
  72.         exit 1
  73. fi
  74.  
  75. # Parse out the arguments based on associated switches
  76. while getopts c:d:p:r: option
  77.         do
  78.         case "${option}"
  79.                 in
  80.                 c) compiler_options_append=${OPTARG}
  81.                    option_c=1;;
  82.                 r)
  83.                    compiler_options_replace=${OPTARG}
  84.                    option_r=1;;
  85.                 d) source_directory=${OPTARG};;
  86.                 p) qb64_program=${OPTARG};;
  87.         esac
  88. done
  89.  
  90. # Build the QB64 Source and Object variables for qb64 command line
  91. if [ "$compiler_options_append" == "--help" ] || [ "$compiler_options_replace" == "--help" ]; then      # Display and execute the qb64 compiler help text
  92.         echo
  93.         echo "qb64 --help"
  94.         echo
  95.         qb64 --help
  96. else                                                                                                   
  97.         if [ "$source_directory" == "" ]; then                          # If source directory isn't provided, then set current working directory as source directory    
  98.                 source_directory="$PWD"
  99.         fi
  100.         qb64_object=$source_directory"/""$qb64_program"
  101.         qb64_source=$source_directory"/""$qb64_program".bas
  102.         FILE=$qb64_source
  103.         is_file "$FILE"                                     # Check to make sure directory/file exists and is readable (not a device or directory only)
  104.         echo
  105.         if [ "$option_c" = 1 ]; then                                                    # Display and the qb64 command line compiler options passed to script
  106.                 echo "Compiler Options (Append): "$compiler_options_append
  107.         elif [ "$option_r" = 1 ]; then
  108.                 echo "Compiler Options (Replace): "$compiler_options_replace
  109.         fi
  110.         echo "Source Code Directory: "$source_directory
  111.         echo "Object Filename: "$qb64_object
  112.         echo "Source Code Filename: "$qb64_source
  113.         echo
  114.  
  115. # Logic to determine the type of process (compile or load QB64 IDE) and execute qb64 binary
  116.         if [ "$compiler_options_append" == "" ] && [ "$option_c" != 1 ]; then   # Check for compiler_options_append equal to NULL, execute replace if so  
  117.                 if [ "$compiler_options_replace" == "" ]; then                                      # If compiler_options_replace is also NULL, then load source into QB64 IDE and edit
  118.                         echo "Editing (QB64 IDE) "$qb64_source" ..."
  119.                         qb64 "$qb64_source"
  120.                         exit 0                                                                          # Exit script after QB64 IDE shuts down
  121.                 else                                                                                    # Execute replace compiler options
  122.                         echo "qb64 $compiler_options_replace -o $qb64_object $qb64_source"
  123.                         echo
  124.                         echo "Compiling "$qb64_source" ..."
  125.                         qb64 "$compiler_options_replace" -o "$qb64_object" "$qb64_source"
  126.                 fi
  127.         else                                                                                            # Eexecute append compiler options
  128.                 if [ "$compiler_options_append" != "" ]; then
  129.                         echo "qb64 -x -w $compiler_options_append -o $qb64_object $qb64_source"
  130.                         echo
  131.                         echo "Compiling "$qb64_source" ..."
  132.                         qb64 -x -w "$compiler_options_append" -o "$qb64_object" "$qb64_source"
  133.                 else
  134.                         echo "qb64 -x -w -o $qb64_object $qb64_source"
  135.                         echo
  136.                         echo "Compiling "$qb64_source" ..."
  137.                         qb64 -x -w -o "$qb64_object" "$qb64_source"
  138.                 fi
  139.         fi
  140.  
  141. # Check for successful compiled completion and display appropriate message
  142.         if [ $? != 0 ]; then                                                               
  143.                 echo ""
  144.                 echo "*** ERROR: Program did not compile successfully. Please fix and retry. Script Terminated."
  145.                 exit 1
  146.         else
  147.                 echo
  148.                 echo "Program \"$qb64_program.bas\" compiled successfully."
  149.         fi
  150. fi
  151.  
« Last Edit: June 29, 2021, 11:21:16 am by George McGinn »
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Update: QB64 Command Line Compiler Script
« Reply #1 on: June 27, 2021, 11:31:29 pm »
I have updated the original post with a new script.

The script now processes the --help argument correctly.

Also, some clarifications: The script will work when running as ROOT. But if the QB64 binary is in a ROOT directory, that will not work properly. Since I am not yet testing for this, the current script will not run if it is ROOT.
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: QB64 Command Line Compiler Script
« Reply #2 on: June 29, 2021, 11:31:56 am »
I have updated the post with the script source to contain the final (for now) bash shell script.

There is also a GitHub repository that will be added to and updated when needed. You can find it at: https://github.com/GeorgeMcGinn/CompileQB64

The script now has the following functionality (Check the readme.md file in GitHub):
    The ability to add to or replace the compiler options
    To bring up the --help when passed
    To compile with just the default options (-x -w)
    To invoke QB64's IDE and load your source code
    To use the current working directory as input into the script (for compiling and editing)
    Verifies that the directory/file is valid before executing qb64 binary
This script does not require you to be in the source directory in order to compile or edit a source when you run from the Terminal program.

A MAC and Windows version will be posted here and on Git when they become available.


____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: QB64 Command Line Compiler Script
« Reply #3 on: June 30, 2021, 03:56:06 am »
I think that the "windows version" should be a little programm that select the bas file, create a batch and launch it with the shell command.
Perhaps in option launch after "UPX" (Ultimate Packer for eXecutables).
 
Thank you for recalling here the possibility of command lines with QB64
Why not yes ?

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: QB64 Command Line Compiler Script
« Reply #4 on: June 30, 2021, 08:38:49 am »
@euklides

I've not had good luck with QB64 executables that have been compressed by UPX. They seem to often misbehave.
Shuwatch!