Skip to content

Commit

Permalink
Open source Diggles SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
ktksgit committed Jul 8, 2020
1 parent f85edfe commit bf85f88
Show file tree
Hide file tree
Showing 945 changed files with 397,068 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# DIGGLES SDK

## How to build

It is only tested to build with Python 3.6 32bit
So
* install Python 32 bit or use the one from the zip
* install scons
* Run the build `scons platform=windows -j8 --nodebug --python-base-path=D:/Python36-x86 --python-lib=python36`


## How to use pip

Run `bin/python36/python.exe -m pip install --upgrade --force-reinstall pip`
Now you should be able to run `bin/python36/scripts/pip3.exe`
188 changes: 188 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import os

AddOption('--mingw',
dest='mingw',
action='store_true',
default=True,
help='mingw build (defaults to gcc)')

AddOption('--nodebug',
dest='nodebug',
action='store_true',
default=False,
help='perform release build')

AddOption('--releasedebug',
dest='releasedebug',
action='store_true',
default=False,
help='perform debug release build')

AddOption('--python-base-path',
dest='python_base_path',
help=r'Python root directory (Like D:\Python38)')

AddOption('--python-lib',
dest='python_lib',
help=r'Python library to link against (e.g. "python38")')


vars = Variables()
vars.Add(
EnumVariable(
"platform",
"Target platform",
"",
allowed_values=("linux", "windows"),
)
)

is_mingw = GetOption('mingw')
nodebug = GetOption('nodebug')
releasedebug = GetOption('releasedebug')
python_base_path = GetOption('python_base_path')
python_lib = GetOption('python_lib')

if not os.path.exists(python_base_path):
Exit(f"{python_base_path} not found")

if is_mingw == True:
env = Environment(
variables=vars,
tools = ['mingw']
)
else:
env = Environment(
variables=vars,
TARGET_ARCH='x86'
)
print ('Visual Studio build')
env.AppendUnique(CXXFLAGS =['/EHsc'])

if env["platform"] == "windows":
env["windows"] = True
windows = True
python_include_path = os.path.join(python_base_path, 'include')
capstone_include_path = os.path.join(python_base_path, 'Lib/site-packages/capstone/include')
capstone_lib_path = os.path.join(python_base_path, 'Lib/site-packages/capstone/lib')
Install(Dir('#/bin'), capstone_lib_path + '/capstone.dll')
if not os.path.exists(capstone_include_path):
Exit(f"Capstone not found at {capstone_lib_path}: pip install capstone")
else:
env["windows"] = False
windows = False
python_include_path = "/usr/include/python3.6m"

# custom methods
def pre_process(env, source):
env = env.Clone()
env.Replace(OBJSUFFIX = '.E')
env.AppendUnique(CCFLAGS = '-E')
return env.Object(source)
env.AddMethod(pre_process, 'PreProcess')

if is_mingw:
env.AppendUnique(CXXFLAGS=[#'-m32',
'-fPIC',
"-std=c++17"
])
env.AppendUnique(CFLAGS=[# '-m32',
'-fPIC',
])
if releasedebug:
env.AppendUnique(CXXFLAGS=['-g', '-O3'])
env.AppendUnique(CFLAGS=['-g'])
elif nodebug:
env.AppendUnique(CXXFLAGS=['-O3'])
else:
env.AppendUnique(CXXFLAGS=['-g'])
env.AppendUnique(CFLAGS=['-g'])
else:
if nodebug or releasedebug:
env.AppendUnique(CPPFLAGS =['/W3', '/MD', '/Od'],#, '/Gs'],
LINKFLAGS=['/RELEASE']
)
else:
env.AppendUnique(CPPFLAGS =['/DEBUG', '/W3', '/MDd', '/Od'],
LINKFLAGS=['/DEBUG']
)
env['CCPDBFLAGS'] = '/Zi /Fd${TARGET}.pdb'
env['PDB'] = '${TARGET.base}.pdb'

Export('env')

# distinguish between release/release_debug/debug builds
if releasedebug:
variant_dir = "object/release_debug"
elif nodebug:
variant_dir = "object/release"
else:
variant_dir = "object/debug"

VariantDir(f'{variant_dir}/libs', 'libs', duplicate=0)

env.Tool('compile_commands')

# Build tcl
(tcl_includes,
tcl_lib_name,
tcl_bin_install_dir,
tclStubLib_obj)= env.SConscript('libs/tcl8.3.2/SConscript_main.py',
exports='nodebug is_mingw windows'
)

tcl_dll_full_path = os.path.join(tcl_bin_install_dir, tcl_lib_name + '.dll')

env.AppendUnique(CPPPATH = [capstone_include_path,
tcl_includes],
# CPPDEFINES = {'__thiscall' : ''},
LIBS = [tcl_lib_name],
LIBPATH = [tcl_bin_install_dir, capstone_lib_path])

pyenv = env.Clone()
pyenv.AppendUnique(CPPPATH=[python_include_path],
LIBS = [python_lib],
LIBPATH = [os.path.join(python_base_path, 'libs')]
)
Export('pyenv')

(tclpython_dll,
static_cpptcl,
static_cpptcl_no_stubs,
py3_obj,
cpptcl_includes,
pybind11_includes) = pyenv.SConscript(f'{variant_dir}/libs/libs.SConscript',
exports='tclStubLib_obj',
)

all_targets = [
static_cpptcl,
static_cpptcl_no_stubs,
py3_obj
]

env.CompileCommands('object', all_targets)

if is_mingw and env["windows"]:
install_dir = Dir('#/bin').abspath
env.Install(install_dir, tcl_dll_full_path)

path = os.environ['PATH']
path = [x for x in path.split(';') if 'mingw32' in x][0]

libstdcpp_6_dll = os.path.join(path, 'libstdc++-6.dll')
env.Install(install_dir, libstdcpp_6_dll)


if any([x in path for x in ['7.2.0','7.3.0', '8.1.0']]):

libgcc_s_dw2_1_dll = os.path.join(path, 'libgcc_s_dw2-1.dll')
env.Install(install_dir, libgcc_s_dw2_1_dll)

libwinpthread_1_dll = os.path.join(path, 'libwinpthread-1.dll')
env.Install(install_dir, libwinpthread_1_dll)

if any([x in path for x in ['4.8.5']]):

libgcc_s_sjlj_1_dll = os.path.join(path, 'libgcc_s_sjlj-1.dll')
env.Install(install_dir, libgcc_s_sjlj_1_dll)
42 changes: 42 additions & 0 deletions examples/bootstrap.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
set info_script [ info script ]
if { $info_script == "" } {
error "Use \"source bootstrap.tcl\""
}

if { [catch {puts "Writing stdout to console"} fid] } {
set stdout [open stdout.txt w]
} else {
set stdout stdout
}

set bootstrap_path [ file dirname $info_script ]
set package_bin_path $bootstrap_path/../bin
set python_path $package_bin_path/python36
set jupyter_qtconsole $python_path/scripts/jupyter-qtconsole.exe
puts $stdout "bootstrap_path $bootstrap_path"
puts $stdout "package_bin_path $package_bin_path"
puts $stdout "python_path $python_path"

set env(PATH) "$package_bin_path;$python_path;$env(PATH);"
set env(PYTHONHOME) $python_path

load tclandpython.dll

set py [PythonInterpreter]
$py exec "
import sys
# This makes import from the example directory possible
sys.path.append('$bootstrap_path')
"

# $py import pytcl
# $py import cpptcl

# Use exclusively first:
# $py import gui.console
# $py gui.console.main()

# or second:
# $py import ipy_kernel
# $py eval ipy_kernel.main('$jupyter_qtconsole')
Empty file added examples/gui/__init__.py
Empty file.
77 changes: 77 additions & 0 deletions examples/gui/console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import tkinter as tk
import sys
import threading


class StdPrintRedirector:
def __init__(self, widget, tag="stdout"):
self.widget = widget
self.tag = tag

def write(self, string):
self.widget.configure(state="normal")
self.widget.insert("end", string, (self.tag,))
if self.widget.count('0.0', 'end', 'lines')[0] > 20:
self.widget.delete('0.0', f'2.0')
self.widget.configure(state="disabled")


class Console(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
toolbar = tk.Frame(self)
toolbar.pack(side="top", fill="x")
b3 = tk.Button(self, text="disable print", command=self.disable_print)
b3.pack(in_=toolbar, side="left")
self.text = tk.Text(self, wrap="word")
self.text.pack(side="top", fill="both", expand=True)
self.text.tag_configure("stderr", foreground="#b22222")

self._stdout = sys.stdout
self._stderr = sys.stderr

sys.stdout = StdPrintRedirector(self.text, "stdout")
sys.stderr = StdPrintRedirector(self.text, "stderr")

def disable_print(self):
print('Disabling printing of', __file__)
sys.stdout = self._stdout
sys.stderr = self._stderr


class Runner:
app = None
t = None

@classmethod
def close(cls):
if cls.app:
cls.app.disable_print()
try:
cls.app.quit()
cls.app.destroy()
except RuntimeError as excp:
print(excp)
cls.app = None

if cls.t:
cls.t.join()
cls.t = None

@classmethod
def open(cls):
cls.t = threading.Thread(
target=Runner(),
)
cls.t.start()

while not cls.app:
pass

def __call__(self):
Runner.app = Console()
Runner.app.mainloop()


def main():
Runner.open()
6 changes: 6 additions & 0 deletions examples/install_requirements.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cd ../bin/python36
python.exe -m pip install --upgrade --force-reinstall pip
cd scripts
pip install jupyter
pip install pyqt5
pip install ptvsd
41 changes: 41 additions & 0 deletions examples/ipy_kernel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# import ptvsd; ptvsd.enable_attach(); ptvsd.wait_for_attach()

# import pydevd
# pydevd.settrace()

import os
import sys
import tempfile
import threading
import time

import gui.console

import IPython

connection_file = os.path.join(
tempfile.gettempdir(),
'connection-{:d}.json'.format(os.getpid()))


def runner(jupyter_qtconsole):
while not os.path.exists(connection_file):
time.sleep(1)
print(f"Starting {connection_file}")
os.system(f"cmd /c {jupyter_qtconsole} --existing {connection_file}")
print("runner started")


def main(jupyter_qtconsole):
gui.console.Runner.close()

t = threading.Thread(
target=runner,
args=[jupyter_qtconsole])
t.start()

IPython.embed_kernel(
local_ns=sys._getframe(1).f_locals,
connection_file=connection_file,
)
t.join()
Loading

0 comments on commit bf85f88

Please sign in to comment.