Browse Source

Try to get cmake working under Python.

master
poikilos 5 years ago
parent
commit
09d810c731
  1. 3
      doc/mtcompile-program-local.md
  2. 10
      install-mts.sh
  3. 208
      mtcompile-program-local.py

3
doc/mtcompile-program-local.md

@ -12,7 +12,7 @@
## Known Issues ## Known Issues
- [ ] Detect the number of cores instead of using `NUMJOBS = 2` - [ ] Detect the number of cores instead of using `NUMJOBS = 3`
(enforce a range of 1 to 3 like the original?) (enforce a range of 1 to 3 like the original?)
@ -55,6 +55,7 @@ Then manually fix uses of:
- [x] Implement a `pushd` function - [x] Implement a `pushd` function
- [x] Implement a `popd` function - [x] Implement a `popd` function
- [x] Implement a `GetOptions` function - [x] Implement a `GetOptions` function
- [x] (See new dict: `mapLongArgs`) Manually map argument aliases.
- [x] use triple quotes for instances of `END` or other self-defined - [x] use triple quotes for instances of `END` or other self-defined
stream end after `<<` stream end after `<<`
- [x] implement a `RunCmd` function - [x] implement a `RunCmd` function

10
install-mts.sh

@ -117,7 +117,7 @@ PATCH_BUILD=$REPO_PATH/mtcompile-program-local.py
for var in "$@" for var in "$@"
do do
if [[ $var = --minetest* ]]; then if [[ $var = --MT_SRC* ]]; then
custom_src_option="$var" custom_src_option="$var"
elif [ "@$var" = "@--client" ]; then elif [ "@$var" = "@--client" ]; then
ENABLE_CLIENT=true ENABLE_CLIENT=true
@ -216,8 +216,9 @@ END
cd "$EXTRACTED_SRC_PATH" || customExit "cd \"$EXTRACTED_SRC_PATH\" failed." cd "$EXTRACTED_SRC_PATH" || customExit "cd \"$EXTRACTED_SRC_PATH\" failed."
if [ ! -z "$custom_src_option" ]; then if [ ! -z "$custom_src_option" ]; then
if [ ! -f "$PATCH_BUILD" ]; then if [ ! -f "$PATCH_BUILD" ]; then
customExit "$PATCH_BUILD must exist when using the --minetest=<path> (custom local copy of minetest source) option") customExit "$PATCH_BUILD must exist when using the --MT_SRC=<path> (custom local copy of minetest source) option"
fi fi
echo "* starting PATCH_BUILD ($PATCH_BUILD build $server_option $extra_options $custom_src_option"
$PATCH_BUILD build $server_option $extra_options $custom_src_option >& program.log $PATCH_BUILD build $server_option $extra_options $custom_src_option >& program.log
elif [ -f "mtcompile-program.pl" ]; then elif [ -f "mtcompile-program.pl" ]; then
# perl mtcompile-program.pl build >& program.log # perl mtcompile-program.pl build >& program.log
@ -237,10 +238,13 @@ END
echo echo
fi fi
fi fi
if [ $? -ne 0 ]; then
customExit "Building failed. See `pwd`/program.log"
fi
end=`date +%s` end=`date +%s`
compile_time=$((end-start)) compile_time=$((end-start))
echo "Compiling the program finished in $compile_time seconds." echo "Compiling the program finished in $compile_time seconds."
cp $EXTRACTED_SRC_PATH/release.txt $EXTRACTED_SRC_PATH/minetest/ || customWarn "Cannot copy $EXTRACTED_SRC_PATH/release.txt to $EXTRACTED_SRC_PATH/minetest/" cp $EXTRACTED_SRC_PATH/release.txt $EXTRACTED_SRC_PATH/minetest/ || customWarn "[install-mts.sh] Cannot copy $EXTRACTED_SRC_PATH/release.txt to $EXTRACTED_SRC_PATH/minetest/"
else else
echo "* using existing $EXTRACTED_SRC_PATH/minetest..." echo "* using existing $EXTRACTED_SRC_PATH/minetest..."
fi fi

208
mtcompile-program-local.py

@ -53,10 +53,15 @@ def isExecutableFile(path):
def which(prog): def which(prog):
# for thisPath in os.environ["PATH"].split(os.pathsep): """
for thisPath in sys.path: Check for the given file within os.environ["PATH"], which contains
# ^ sys.path includes BINDIR added before calling this! paths separated by os.pathsep.
NOTE: sys.path is NOT applicable, since it only has Python paths.
"""
for thisPath in os.environ["PATH"].split(os.pathsep):
sub_path = os.path.join(thisPath, prog) sub_path = os.path.join(thisPath, prog)
if os.path.isfile(sub_path):
return sub_path
return "" return ""
@ -189,7 +194,7 @@ Option switches:
Aliases: --usage Aliases: --usage
For full documentation, see "linux-minetest-kit.txt". For full documentation, see "linux-minetest-kit.txt".
--minetest=<minetest> # Set the minetest source path to a --MT_SRC=<minetest> # Set the minetest source path to a
# locally-modified copy, such as # locally-modified copy, such as
# $HOME/git/minetest # $HOME/git/minetest
@ -254,7 +259,7 @@ else:
GITURL = 'http://git.minetest.org/minetest/minetest.git' GITURL = 'http://git.minetest.org/minetest/minetest.git'
IE = 'Internal error' IE = 'Internal error'
FlagMinetest = None Flags = {}
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# global variables # global variables
@ -312,7 +317,11 @@ def popd():
def RunCmd(cmdParts, exitOnFail=True): def RunCmd(cmdParts, exitOnFail=True):
child = subprocess.Popen(openRTSP+opts.split(), # Popen can get confused when arguments contain quotes
# (see <https://stackoverflow.com/questions/14928860/passing-double-
# quote-shell-commands-in-python-to-subprocess-popen>) such as
# in "-DCMAKE_CXX_FLAG="+XCFLAGS where XCFLAGS contains quotes.
child = subprocess.Popen(cmdParts, shell=True,
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
streamdata = child.communicate()[0] streamdata = child.communicate()[0]
rc = child.returncode rc = child.returncode
@ -339,16 +348,14 @@ def GetProgDir():
return os.getcwd() return os.getcwd()
def GetOptions(nonBoolNames=[], TitleCaseAndFlag=False, bareArgs=[]): def GetOptions(nonBoolNames=[], bareArgs=[]):
""" """
Convert command-line arguments to globals. Convert command-line arguments to values in the global Flags dict.
Keyword Arguments: Keyword Arguments:
nonBoolNames -- You should set all of these to None in the global nonBoolNames -- Specify which arguments can have values. All others
namespace before calling this so that you can check them against are considered boolean, and will end the program if they contain
None later without getting an undefined variable error. '='.
TitleCaseAndFlag -- Change '--something' to 'FlagSomething' if
TitleCaseAndFlag, otherwise change it to 'something'.
bareArgs -- allowed arguments without "--" at the beginning. bareArgs -- allowed arguments without "--" at the beginning.
Returns: Returns:
@ -371,9 +378,10 @@ def GetOptions(nonBoolNames=[], TitleCaseAndFlag=False, bareArgs=[]):
" {}.".format(nonBoolNames)) " {}.".format(nonBoolNames))
return False return False
val = arg[signI+1:] val = arg[signI+1:]
if TitleCaseAndFlag: # if TitleCaseAndFlag:
name = "Flag" + name.title() # name = "Flag" + name.title()
globals()[name] = val print("* {} = {}".format(name, val))
Flags[name] = val
else: else:
print("The option is unknown: {}".format(arg)) print("The option is unknown: {}".format(arg))
return False return False
@ -387,7 +395,7 @@ USAGE_FMT = """
""" """
def UsageText(): def UsageText(msg=""):
""" """
"UsageText" prints usage text for the current program, then termin- "UsageText" prints usage text for the current program, then termin-
ates the program with exit status one. ates the program with exit status one.
@ -401,6 +409,10 @@ def UsageText():
)) ))
# USAGE_TEXT = evalSed('s@\s*\z@\n@s') # USAGE_TEXT = evalSed('s@\s*\z@\n@s')
print(THIS_USAGE) print(THIS_USAGE)
print("")
print(msg)
print("")
print("")
exit(1) exit(1)
@ -425,23 +437,23 @@ def main():
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Command-line option flags. # Command-line option flags.
FlagBuild = False Flags["build"] = False
FlagClient = False Flags["client"] = False
FlagDebug = False Flags["debug"] = False
FlagEdgy = False Flags["edgy"] = False
FlagGitPull = False Flags["gitpull"] = False
FlagGitReset = False Flags["gitreset"] = False
FlagHelp = False Flags["help"] = False
FlagMakeProd = False Flags["makeprod"] = False
FlagFakeMT4 = False Flags["fakemt4"] = False
FlagNoClean = False Flags["noclean"] = False
FlagPortable = False Flags["portable"] = False
FlagPostgres = False Flags["postgres"] = False
FlagRedis = False Flags["redis"] = False
FlagSafe = False Flags["safe"] = False
FlagServer = False Flags["server"] = False
FlagOldProto = True Flags["oldproto"] = True
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Initial setup. # Initial setup.
@ -461,21 +473,35 @@ def main():
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Parse command-line arguments. # Parse command-line arguments.
if not GetOptions(nonBoolNames=["minetest"], TitleCaseAndFlag=True, if not GetOptions(nonBoolNames=["MT_SRC"], bareArgs=["build"]):
bareArgs=["build"]):
UsageText() UsageText()
mapLongArgs = {}
mapLongArgs["edgytest"] = "edgy"
mapLongArgs["notidy"] = "noclean"
mapLongArgs["oldprotocol"] = "oldproto"
mapLongArgs["postgresql"] = "postgres"
mapLongArgs["usage"] = "help"
for k, v in mapLongArgs.items():
old_v = Flags.get(k)
if old_v is not None:
Flags[v] = old_v
del Flags[k]
# Handle usage-text exit # Handle usage-text exit
if (not FlagBuild) or FlagHelp: if (not Flags["build"]) or Flags["help"]:
UsageText() if not Flags["build"]:
msg = ("You did not specify build or --build (got:{})."
"".format(Flags))
UsageText(msg=msg)
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Handle misc. flag issues. # Handle misc. flag issues.
if FlagEdgy: if Flags["edgy"]:
FlagFakeMT4 = True Flags["fakemt4"] = True
if FlagEdgy or FlagFakeMT4: if Flags["edgy"] or Flags["fakemt4"]:
FlagOldProto = True Flags["oldproto"] = True
if FlagEdgy and FlagGitPull: if Flags["edgy"] and Flags["gitpull"]:
customExit("Error: Can't use both --edgy and --gitpull") customExit("Error: Can't use both --edgy and --gitpull")
#--------------------------------------------------------------------- #---------------------------------------------------------------------
@ -502,15 +528,16 @@ which contains the "mtsrc" directory.
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Misc. setup. # Misc. setup.
sys.path.insert(0, BINDIR) if not BINDIR in os.environ["PATH"]:
os.environ["PATH"] = BINDIR + os.pathsep + os.environ["PATH"]
which("g++") which("g++")
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Handle some of the option flags. # Handle some of the option flags.
if FlagDebug: if Flags["debug"]:
MAKEDEBUG = True MAKEDEBUG = True
if FlagMakeProd: if Flags["makeprod"]:
MAKEPROD = True MAKEPROD = True
if FlagNoClean: if Flags["noclean"]:
TIDYUP = False TIDYUP = False
if MAKEPROD: if MAKEPROD:
@ -521,11 +548,11 @@ which contains the "mtsrc" directory.
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Handle "--gitreset". # Handle "--gitreset".
if FlagGitReset: if Flags["gitreset"]:
if FlagGitPull: if Flags["gitpull"]:
customExit("Error: Can't use both --gitreset and --gitpull\n") customExit("Error: Can't use both --gitreset and --gitpull\n")
if FlagSafe and os.path.isdir('minetest'): if Flags["safe"] and os.path.isdir('minetest'):
print(""" print("""
Error: "minetest" directory exists and "--gitreset" needs to delete Error: "minetest" directory exists and "--gitreset" needs to delete
it. But can't because "--safe" was specified. If you wish to proceed, it. But can't because "--safe" was specified. If you wish to proceed,
@ -547,8 +574,8 @@ move or rename the directory.
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Handle "--gitpull". # Handle "--gitpull".
if FlagGitPull: if Flags["gitpull"]:
if FlagGitReset: if Flags["gitreset"]:
customExit("Error: Can't use both --gitreset and --gitpull\n") customExit("Error: Can't use both --gitreset and --gitpull\n")
TIDYUP = False TIDYUP = False
@ -593,11 +620,11 @@ automatically.
client_line = "-DBUILD_CLIENT=1" client_line = "-DBUILD_CLIENT=1"
server_line = "-DBUILD_SERVER=1" server_line = "-DBUILD_SERVER=1"
if FlagClient and not FlagServer: if Flags["client"] and not Flags["server"]:
client_line = "-DBUILD_CLIENT=1" client_line = "-DBUILD_CLIENT=1"
server_line = "-DBUILD_SERVER=0" server_line = "-DBUILD_SERVER=0"
if not FlagClient and FlagServer: if not Flags["client"] and Flags["server"]:
client_line = "-DBUILD_CLIENT=0" client_line = "-DBUILD_CLIENT=0"
server_line = "-DBUILD_SERVER=1" server_line = "-DBUILD_SERVER=1"
@ -616,7 +643,7 @@ automatically.
postgres_line = "-DENABLE_POSTGRESQL=0" postgres_line = "-DENABLE_POSTGRESQL=0"
if FlagPostgres: if Flags["postgres"]:
print("* postgres (due to --postgresql)") print("* postgres (due to --postgresql)")
postgres_line = "-DENABLE_POSTGRESQL=1" postgres_line = "-DENABLE_POSTGRESQL=1"
else: else:
@ -627,7 +654,7 @@ automatically.
redis_line = "-DENABLE_REDIS=0" redis_line = "-DENABLE_REDIS=0"
if FlagRedis: if Flags["redis"]:
print("* redis (due to --redis)") print("* redis (due to --redis)")
redis_line = "-DENABLE_REDIS=1" redis_line = "-DENABLE_REDIS=1"
else: else:
@ -662,11 +689,12 @@ gcc-bootstrap mode enabled.
RESETDIR = (not os.path.isdir(PRODDIR)) or TIDYUP RESETDIR = (not os.path.isdir(PRODDIR)) or TIDYUP
if RESETDIR: if RESETDIR:
if FlagSafe and os.path.isdir('minetest'): if Flags["safe"] and os.path.isdir('minetest'):
print(""" print("""
Error: We need to delete the existing "minetest" directory, but Error: We need to delete the existing "minetest" directory, but
"--safe" is specified. If you'd like to preserve the directory, move#or rename it. Otherwise, drop the "--safe" switch. "--safe" is specified. If you'd like to preserve the directory, move
or rename it. Otherwise, drop the "--safe" switch.
""") """)
exit(1) exit(1)
# PRODDIR is THISDIR/minetest # PRODDIR is THISDIR/minetest
@ -679,19 +707,23 @@ Error: We need to delete the existing "minetest" directory, but
continue continue
if sub_path == PRODDIR: if sub_path == PRODDIR:
# ^ sub_path must be generated the same way as # ^ sub_path must be generated the same way as
# PRODDIR (from THISDIR) for this to work. # PRODDIR (from THISDIR) for this to work (to
# remove the linux-minetest-kit/minetest directory).
print("* removing old \"{}\"".format(sub_path))
shutil.rmtree(sub_path) shutil.rmtree(sub_path)
elif sub.startswith("minetest-newline"): elif sub.startswith("minetest-newline"):
shutil.rmtree(sub_path) shutil.rmtree(sub_path)
print("* extracting in " + os.getcwd()) print("* extracting in " + os.getcwd())
mtNewLine = "minetest-newline" mtNewLine = "minetest-newline"
if FlagEdgy: if Flags["edgy"]:
mtNewLine = "minetest-newline" mtNewLine = "minetest-newline"
if FlagMinetest is not None: if Flags.get("MT_SRC") is not None:
if not os.path.isdir(FlagMinetest): if not os.path.isdir(Flags["MT_SRC"]):
customExit("{} does not exist.".format(FlagMinetest)) customExit("{} does not exist.".format(Flags["MT_SRC"]))
print("* using \"{}\"".format(FlagMinetest)) print("* using \"{}\" (copying to \"{}\")"
shutil.copytree(FlagMinetest, PRODDIR, copy_function=copy2) "".format(Flags["MT_SRC"], PRODDIR))
shutil.copytree(Flags["MT_SRC"], PRODDIR,
copy_function=shutil.copy2)
else: else:
tarPath = os.path.join(BALLDIR, mtNewLine + ".tar.bz2") tarPath = os.path.join(BALLDIR, mtNewLine + ".tar.bz2")
tar = tarfile.open(tarPath) tar = tarfile.open(tarPath)
@ -776,14 +808,14 @@ again.
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Set "$XCFLAGS" (extra compiler flags). # Set "$XCFLAGS" (extra compiler flags).
XCFLAGS = "-O2 -I" + INCDIR + "" XCFLAGS = "-O2 -I" + INCDIR
if MAKEDEBUG: if MAKEDEBUG:
XCFLAGS = XCFLAGS + " -g" XCFLAGS += " -g"
if not PORTABLE: if not PORTABLE:
XCFLAGS = "-march=native " + XCFLAGS XCFLAGS = "-march=native " + XCFLAGS
XCFLAGS = XCFLAGS + " -Wl,-L" + LIBDIR + " -Wl,-R" + LIBDIR XCFLAGS += " -Wl,-L" + LIBDIR + " -Wl,-R" + LIBDIR
if os.path.isdir(LIB64DIR): if os.path.isdir(LIB64DIR):
XCFLAGS = "$XCFLAGS -Wl,-L" + LIB64DIR + " -Wl,-R" + LIB64DIR XCFLAGS += " -Wl,-L" + LIB64DIR + " -Wl,-R" + LIB64DIR
print("XCFLAGS="+XCFLAGS) print("XCFLAGS="+XCFLAGS)
@ -793,17 +825,23 @@ again.
WHICH_GCC = which("gcc") WHICH_GCC = which("gcc")
WHICH_GPP = which("g++") WHICH_GPP = which("g++")
if not isExecutableFile(WHICH_GCC): if not isExecutableFile(WHICH_GCC):
customExit("gcc is not present or not executable.") if WHICH_GCC is not None:
print("gcc: {}".format(WHICH_GCC))
customExit("gcc is not present or not executable in {}."
"".format(os.environ["PATH"]))
if not isExecutableFile(WHICH_GPP): if not isExecutableFile(WHICH_GPP):
customExit("g++ is not present or not executable.") if WHICH_GPP is not None:
print("g++: {}".format(WHICH_GPP))
customExit("g++ is not present or not executable in {}."
"".format(os.environ["PATH"]))
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Handle another "--edgy step". # Handle another "--edgy step".
if FlagEdgy: if Flags["edgy"]:
CM = 'src/defaultsettings.cpp' CM = 'src/defaultsettings.cpp'
# TODO: finish converting this from perl # TODO: finish converting this from perl
print("FlagEdgy changing {} is not yet implemented." print("edgy changing {} is not yet implemented."
"".format(CM)) "".format(CM))
data = None data = None
try: try:
@ -825,10 +863,10 @@ again.
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Handle "--fakemt4". # Handle "--fakemt4".
if FlagFakeMT4: if Flags["fakemt4"]:
CM = 'CMakeLists.txt' CM = 'CMakeLists.txt'
print("FlagFakeMT4 changing {} is not yet implemented." print("fakemt4 changing {} is not yet implemented."
"".format(CM)) "".format(CM))
# TODO: handle fakemt4 # TODO: handle fakemt4
# data = None # data = None
@ -855,9 +893,9 @@ again.
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Handle "--oldproto". # Handle "--oldproto".
if FlagOldProto: if Flags["oldproto"]:
CM = 'src/network/networkprotocol.h' CM = 'src/network/networkprotocol.h'
print("FlagOldProto changing {} is not yet implemented." print("oldproto changing {} is not yet implemented."
"".format(CM)) "".format(CM))
# TODO: change protocol in CM: # TODO: change protocol in CM:
# data = None # data = None
@ -902,6 +940,12 @@ again.
cmdParts.append("-DCMAKE_CXX_FLAGS=\"{}\"".format(XCFLAGS)) cmdParts.append("-DCMAKE_CXX_FLAGS=\"{}\"".format(XCFLAGS))
cmdParts.append("-DCMAKE_C_FLAGS_RELEASE=\"{}\"".format(XCFLAGS)) cmdParts.append("-DCMAKE_C_FLAGS_RELEASE=\"{}\"".format(XCFLAGS))
cmdParts.append("-DCMAKE_CXX_FLAGS_RELEASE=\"{}\"".format(XCFLAGS)) cmdParts.append("-DCMAKE_CXX_FLAGS_RELEASE=\"{}\"".format(XCFLAGS))
print("")
print("")
print("Running cmake in {}:".format(os.getcwd()))
print(" ".join(cmdParts))
print("")
print("")
RunCmd(cmdParts); RunCmd(cmdParts);
# TODO: use some absolute pathnames as the Perl version does # TODO: use some absolute pathnames as the Perl version does
@ -918,11 +962,11 @@ again.
# -i `find . -type f -name link.txt` # -i `find . -type f -name link.txt`
# END # END
# cmd = &FixStr ( + cmd + ) # cmd = &FixStr ( + cmd + )
#&RunCmd ($cmd); # RunCmd (cmd.split(" ")) #TODO use cmdParts after editing it
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Build the program. # Build the program.
NUMJOBS = 2 NUMJOBS = 3
RunCmd(["make", "clean"]) RunCmd(["make", "clean"])
RunCmd("make", "-j{}".format(NUMJOBS)) RunCmd("make", "-j{}".format(NUMJOBS))
serverlistDir = os.path.join(PRODDIR, "client", "serverlist") serverlistDir = os.path.join(PRODDIR, "client", "serverlist")
@ -948,8 +992,8 @@ again.
pushd('games'); pushd('games');
cmd = "" cmd = ""
gameNames = ["minimal", "amhi_game"] gameNames = ["minimal", "amhi_game"]
if not FlagEdgy: if not Flags["edgy"]:
# TODO: What does FlagEdgy do here? Does it leave the old # TODO: What does Flags["edgy"] do here? Does it leave the old
# Bucket_Game and do nothing else differently? # Bucket_Game and do nothing else differently?
# See mtcompile-program.pl # See mtcompile-program.pl
gameNames.append("Bucket_Game") gameNames.append("Bucket_Game")
@ -973,7 +1017,7 @@ again.
worldPath = os.path.join(WORLDS_PATH, worldName) worldPath = os.path.join(WORLDS_PATH, worldName)
if os.path.isdir(worldPath): if os.path.isdir(worldPath):
shutil.rmtree(worldPath) shutil.rmtree(worldPath)
if FlagEdgy: if Flags["edgy"]:
continue continue
tarPath = os.path.join(BALLDIR, worldName) tarPath = os.path.join(BALLDIR, worldName)
if os.path.isfile(tarPath): if os.path.isfile(tarPath):

Loading…
Cancel
Save