Example #1
0
        private void CompilePythonModule(string fileName, PythonCompilerSink sink, bool createMain)
        {
            assemblyGen.SetPythonSourceFile(fileName);
            CompilerContext context = new CompilerContext(fileName, sink);
            Parser          p       = Parser.FromFile(state, context);
            Stmt            body    = p.ParseFileInput();

            if (sink.Errors > 0)
            {
                return;
            }

            GlobalSuite gs         = IronPython.Compiler.Binder.Bind(body, context);
            string      moduleName = Path.GetFileNameWithoutExtension(fileName);
            TypeGen     tg         = OutputGenerator.GenerateModuleType(moduleName, assemblyGen);
            CodeGen     init;

            if (!AutoImportAll)
            {
                init = OutputGenerator.GenerateModuleInitialize(context, gs, tg);
            }
            else
            {
                // auto-import all compiled modules, useful for CodeDom scenarios.
                init = OutputGenerator.GenerateModuleInitialize(context, gs, tg, delegate(CodeGen cg) {
                    for (int i = 0; i < sourceFiles.Count; i++)
                    {
                        string otherModName = Path.GetFileNameWithoutExtension(sourceFiles[i]);
                        if (otherModName == moduleName)
                        {
                            continue;
                        }

                        FromImportStmt stmt = new FromImportStmt(
                            new DottedName(new Name[] { Name.Make(otherModName) }),
                            FromImportStmt.Star, null);
                        stmt.start = new Location(1, 1);
                        stmt.end   = new Location(1, 1);
                        stmt.Emit(cg);
                    }
                });
            }

            if (createMain)
            {
                CodeGen main = OutputGenerator.GenerateModuleEntryPoint(tg, init, Path.GetFileNameWithoutExtension(mainFile), referencedAssemblies);
                assemblyGen.SetEntryPoint(main.MethodInfo, targetKind);
            }

            AddPythonModuleAttribute(tg, moduleName);
            tg.FinishType();
        }
Example #2
0
        public void Compile()
        {
            string fullPath = Path.GetFullPath(outputAssembly);
            string outDir   = Path.GetDirectoryName(fullPath);
            string fileName = Path.GetFileName(outputAssembly);

            PythonCompilerSink sink = new PythonCompilerSink(compilerSink);

            assemblyGen = new AssemblyGen(
                Path.GetFileNameWithoutExtension(outputAssembly),
                outDir, fileName, includeDebugInformation, executable, machine
                );

            bool entryPointSet = false;

            // set default main file
            if (mainFile == null && sourceFiles.Count == 1 && targetKind != PEFileKinds.Dll)
            {
                mainFile = sourceFiles[0];
            }

            foreach (string sourceFile in sourceFiles)
            {
                bool createMainMethod = sourceFile == mainFile;
                CompilePythonModule(sourceFile, sink, createMainMethod);

                if (sink.Errors > 0)
                {
                    return;
                }

                if (createMainMethod)
                {
                    entryPointSet = true;
                }
            }

            if (resourceFiles != null)
            {
                foreach (ResourceFile rf in resourceFiles)
                {
                    assemblyGen.AddResourceFile(rf.Name, rf.File, rf.PublicResource ? ResourceAttributes.Public : ResourceAttributes.Private);
                }
            }

            if (targetKind != PEFileKinds.Dll && !entryPointSet)
            {
                sink.AddError("", string.Format("Need an entry point for target kind {0}", targetKind), -1, Severity.Error);
            }

            assemblyGen.Dump();
        }
        private void CompilePythonModule(string fileName, PythonCompilerSink sink, bool createMain)
        {
            assemblyGen.SetPythonSourceFile(fileName);
            CompilerContext context = new CompilerContext(fileName, sink);
            Parser p = Parser.FromFile(state, context);
            Statement body = p.ParseFileInput();

            if (sink.Errors > 0) return;

            GlobalSuite gs = Compiler.Ast.Binder.Bind(body, context);
            string moduleName = GetModuleFromFilename(fileName);
            TypeGen tg = OutputGenerator.GenerateModuleType(moduleName, assemblyGen);
            CodeGen init = CompileModuleInit(context, gs, tg, moduleName);

            if (createMain) {
                CodeGen main = OutputGenerator.GenerateModuleEntryPoint(tg, init, moduleName, referencedAssemblies);
                assemblyGen.SetEntryPoint(main.MethodInfo, targetKind);
            }

            assemblyGen.AddPythonModuleAttribute(tg, moduleName);
            tg.FinishType();
        }
        public void Compile()
        {
            string fullPath = Path.GetFullPath(outputAssembly);
            string outDir = Path.GetDirectoryName(fullPath);
            string fileName = Path.GetFileName(outputAssembly);

            PythonCompilerSink sink = new PythonCompilerSink(compilerSink);

            assemblyGen = new AssemblyGen(
                Path.GetFileNameWithoutExtension(outputAssembly),
                outDir, fileName, includeDebugInformation, staticTypes, executable, machine
                );

            bool entryPointSet = false;

            // set default main file
            if (mainFile == null && sourceFiles.Count == 1 && targetKind != PEFileKinds.Dll) {
                mainFile = sourceFiles[0];
            }

            foreach (string sourceFile in sourceFiles) {
                bool createMainMethod = sourceFile == mainFile;
                CompilePythonModule(sourceFile, sink, createMainMethod);

                if (sink.Errors > 0) return;

                if (createMainMethod) {
                    entryPointSet = true;
                }
            }

            if (resourceFiles != null) {
                foreach (ResourceFile rf in resourceFiles) {
                    assemblyGen.AddResourceFile(rf.Name, rf.File, rf.PublicResource ? ResourceAttributes.Public : ResourceAttributes.Private);
                }
            }

            if (targetKind != PEFileKinds.Dll && !entryPointSet) {
                sink.AddError("", string.Format("Need an entry point for target kind {0}", targetKind), String.Empty, CodeSpan.Empty, -1, Severity.Error);
            }

            assemblyGen.Dump();
        }