def process(self, contents): out_file = self._main_ino + ".cpp" assert self._gcc_preprocess(contents, out_file) contents = fs.get_file_contents(out_file) contents = self._join_multiline_strings(contents) fs.write_file_contents( out_file, self.append_prototypes(contents), errors="backslashreplace" ) return out_file
def _file_long_data(env, data): build_dir = env.subst("$BUILD_DIR") if not isdir(build_dir): makedirs(build_dir) tmp_file = join(build_dir, "longcmd-%s" % md5(hashlib_encode_data(data)).hexdigest()) if isfile(tmp_file): return tmp_file fs.write_file_contents(tmp_file, data) return tmp_file
def _gcc_preprocess(self, contents, out_file): tmp_path = mkstemp()[1] fs.write_file_contents(tmp_path, contents, errors="backslashreplace") self.env.Execute( self.env.VerboseAction( '$CXX -o "{0}" -x c++ -fpreprocessed -dD -E "{1}"'.format( out_file, tmp_path), "Converting " + basename(out_file[:-4]), )) atexit.register(_delete_file, tmp_path) return isfile(out_file)
def generate_outputcpp(self, test_dir): assert isdir(test_dir) cpp_tpl = "\n".join([ "$include", "#include <output_export.h>", "", "$object", "", "#ifdef __GNUC__", "void output_start(unsigned int baudrate __attribute__((unused)))", "#else", "void output_start(unsigned int baudrate)", "#endif", "{", " $begin;", "}", "", "void output_char(int c)", "{", " $putchar;", "}", "", "void output_flush(void)", "{", " $flush;", "}", "", "void output_complete(void)", "{", " $end;", "}", ]) def delete_tmptest_file(file_): try: remove(file_) except: # pylint: disable=bare-except if isfile(file_): click.secho( "Warning: Could not remove temporary file '%s'. " "Please remove it manually." % file_, fg="yellow", ) tpl = Template(cpp_tpl).substitute( TRANSPORT_OPTIONS[self.get_transport()]) data = Template(tpl).substitute(baudrate=self.get_baudrate()) tmp_file = join(test_dir, "output_export.cpp") fs.write_file_contents(tmp_file, data) atexit.register(delete_tmptest_file, tmp_file)
def init_lib_readme(lib_dir): # pylint: disable=line-too-long fs.write_file_contents( os.path.join(lib_dir, "README"), """ This directory is intended for project specific (private) libraries. PlatformIO will compile them to static libraries and link into executable file. The source code of each library should be placed in a an own separate directory ("lib/your_library_name/[here are source files]"). For example, see a structure of the following two libraries `Foo` and `Bar`: |--lib | | | |--Bar | | |--docs | | |--examples | | |--src | | |- Bar.c | | |- Bar.h | | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html | | | |--Foo | | |- Foo.c | | |- Foo.h | | | |- README --> THIS FILE | |- platformio.ini |--src |- main.c and a contents of `src/main.c`: ``` #include <Foo.h> #include <Bar.h> int main (void) { ... } ``` PlatformIO Library Dependency Finder will find automatically dependent libraries scanning project source files. More information about PlatformIO Library Dependency Finder - https://docs.platformio.org/page/librarymanager/ldf.html """, )
def init_test_readme(test_dir): fs.write_file_contents( os.path.join(test_dir, "README"), """ This directory is intended for PIO Unit Testing and project tests. Unit Testing is a software testing method by which individual units of source code, sets of one or more MCU program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use. Unit testing finds problems early in the development cycle. More information about PIO Unit Testing: - https://docs.platformio.org/page/plus/unit-testing.html """, )
def clean_build_dir(build_dir, config): # remove legacy ".pioenvs" folder legacy_build_dir = join(get_project_dir(), ".pioenvs") if isdir(legacy_build_dir) and legacy_build_dir != build_dir: fs.rmtree(legacy_build_dir) checksum_file = join(build_dir, "project.checksum") checksum = compute_project_checksum(config) if isdir(build_dir): # check project structure if isfile(checksum_file) and fs.get_file_contents(checksum_file) == checksum: return fs.rmtree(build_dir) makedirs(build_dir) fs.write_file_contents(checksum_file, checksum)
def is_prog_obsolete(prog_path): prog_hash_path = prog_path + ".sha1" if not isfile(prog_path): return True shasum = sha1() with open(prog_path, "rb") as fp: while True: data = fp.read(1024) if not data: break shasum.update(data) new_digest = shasum.hexdigest() old_digest = (fs.get_file_contents(prog_hash_path) if isfile(prog_hash_path) else None) if new_digest == old_digest: return False fs.write_file_contents(prog_hash_path, new_digest) return True
def init_include_readme(include_dir): fs.write_file_contents( os.path.join(include_dir, "README"), """ This directory is intended for project header files. A header file is a file containing C declarations and macro definitions to be shared between several project source files. You request the use of a header file in your project source file (C, C++, etc) located in `src` folder by including it, with the C preprocessing directive `#include'. ```src/main.c #include "header.h" int main (void) { ... } ``` Including a header file produces the same results as copying the header file into each source file that needs it. Such copying would be time-consuming and error-prone. With a header file, the related declarations appear in only one place. If they need to be changed, they can be changed in one place, and programs that include the header file will automatically use the new version when next recompiled. The header file eliminates the labor of finding and changing all the copies as well as the risk that a failure to find one copy will result in inconsistencies within a program. In C, the usual convention is to give header files names that end with `.h'. It is most portable to use only letters, digits, dashes, and underscores in header file names, and at most one dot. Read more about using header files in official GCC documentation: * Include Syntax * Include Operation * Once-Only Headers * Computed Includes https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html """, )
def _generate_project_main(_, project_dir, framework): main_content = None if framework == "arduino": main_content = "\n".join([ "#include <Arduino.h>", "", "void setup() {", " // put your setup code here, to run once:", "}", "", "void loop() {", " // put your main code here, to run repeatedly:", "}", "", ]) elif framework == "mbed": main_content = "\n".join([ "#include <mbed.h>", "", "int main() {", "", " // put your setup code here, to run once:", "", " while(1) {", " // put your main code here, to run repeatedly:", " }", "}", "", ]) if not main_content: return project_dir with fs.cd(project_dir): config = ProjectConfig() src_dir = config.get_optional_dir("src") main_path = join(src_dir, "main.cpp") if isfile(main_path): return project_dir if not isdir(src_dir): os.makedirs(src_dir) fs.write_file_contents(main_path, main_content.strip()) return project_dir
def init_cvs_ignore(project_dir): conf_path = os.path.join(project_dir, ".gitignore") if os.path.isfile(conf_path): return fs.write_file_contents(conf_path, ".pio\n")
def init_ci_conf(project_dir): conf_path = os.path.join(project_dir, ".travis.yml") if os.path.isfile(conf_path): return fs.write_file_contents( conf_path, """# Continuous Integration (CI) is the practice, in software # engineering, of merging all developer working copies with a shared mainline # several times a day < https://docs.platformio.org/page/ci/index.html > # # Documentation: # # * Travis CI Embedded Builds with PlatformIO # < https://docs.travis-ci.com/user/integration/platformio/ > # # * PlatformIO integration with Travis CI # < https://docs.platformio.org/page/ci/travis.html > # # * User Guide for `platformio ci` command # < https://docs.platformio.org/page/userguide/cmd_ci.html > # # # Please choose one of the following templates (proposed below) and uncomment # it (remove "# " before each line) or use own configuration according to the # Travis CI documentation (see above). # # # Template #1: General project. Test it using existing `platformio.ini`. # # language: python # python: # - "2.7" # # sudo: false # cache: # directories: # - "~/.platformio" # # install: # - pip install -U platformio # - platformio update # # script: # - platformio run # # Template #2: The project is intended to be used as a library with examples. # # language: python # python: # - "2.7" # # sudo: false # cache: # directories: # - "~/.platformio" # # env: # - PLATFORMIO_CI_SRC=path/to/test/file.c # - PLATFORMIO_CI_SRC=examples/file.ino # - PLATFORMIO_CI_SRC=path/to/test/directory # # install: # - pip install -U platformio # - platformio update # # script: # - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N """, )